• #664
    Refresh Thread
    By: Rodeoclash x Show Full Post
    Reply
    I had a programming test yesterday. On of the questions was:

    Given an array of numbers
    [4, 6, 8, 3]

    Work out how many steps it takes to make them all the same number if you can only increment them by +1/-1 per step.

    I worked out the average of the array then took the difference between the average and the number furtherest away from it. Was this correct?
    Jan 18, 2011 6:34pm PST
    toggle
    I had a programming test yesterday. On of the questions was: Given an array of numbers [4, 6, 8, 3] Wor... : Rodeoclash

    Thread Truncated. Click to see all 31 replies.

    • toggle
      at a glance I imagine median could be the better choice depending on the distribution of numbers in the a... : derelict515This person is cool!
      • toggle
        Yes I believe the number you end up with has to divide the set of numbers perfectly in 2; corner cases as... : jim bone
        • My explanation/proof for this is: If you take the biggest and smallest number, picking any number in b... : jim bone
    • toggle
      Assuming all elements in the array can be worked on concurrently in the same step, yes. : shirif
      • It didn't say but I assumed that : Rodeoclash
    • toggle
      couldn't you take the range and simply divide it by 2 rounding up? : grandmasterleeThis person is cool!
      • toggle
        Consider [1, 1, 1, 1000]: the minimum number of steps is 999 (to make 1000 -> 1). The range (1000-1)... : jim bone
        • If you can act on all the numbers in the array in one step, you can do range / 2. In any case, it's a ... : famine101
    • toggle
      3. : hirezThis person is cool!
      • or 3 the other way : watcherxpThis person is cool!
      • toggle
        thats how many licks it takes to get to the center of a tootsie pop : RobFlinn
        • Very Wise : watcherxpThis person is cool!
    • toggle
      If you adjusted all the numbers to 4 then the number of increments required would be: 0 + 2 + 4 + 1 = 7 ... : VenDrake
      • Looking at previous chat, it seems there's an assumption that you could change all numbers with a single ... : VenDrake
    • toggle
      If you guys want another one from the test: Write a function def triangle(arr) which given a zer... : Rodeoclash
      • My solution (which I don't think is right, although it passed the tests they supplied) (Ruby) def tria... : Rodeoclash
      • Part of it has fallen off, you can see it here: http://www.richardson.co.nz/temp/missing_bit.png : Rodeoclash
      • toggle
        You need to 1. Make a list of all possible combinations of 3 Array indices. 2. Test against your logic. ... : shirif
        • on 1, d'oh well it would be impossible to write function 1 since the algorithm cannot be halted, it does... : shirif
      • For laughs, the solution in MATLAB function out = triangle(arr) n = numel(arr); % compute permutations... : AmadanThis person is cool!
    • write a tree search algorithm and go through the permutations : YanksThis person is cool!
    • toggle
      Well, got my results back. 50% I suck at programming. : Rodeoclash
      • toggle
        nah, these questions suck. : shirif
        • toggle
          I was going for the job as an all rounder. I do graphic design, UX design and Ruby/Rails. I've written so... : Rodeoclash
          • You might still be in the running. Keep it up :) : shirif
          • toggle
            i feel really lost at work doing my new programming job. i feel bad like im just slowing things down and ... : spaceless
            • toggle
              try pressing your turbo button : jim bone
              • i just have slow and slower : spaceless
    • toggle
      One more question on this. If the function could not return the number of steps, it was supposed to retur... : Rodeoclash
      • An example of an invalid set would be an empty array. : VenDrake