Interview 02

Implement Insertion Sort.

Specifications

Feature Tasks

Structure

Familiarize yourself with the grading rubric, so you know how to score the interview.

Look for effective problem solving, efficient use of time, and effective communication with the whiteboard space available.

Every solution might look a little different, but the candidate should be able to test their solution with different inputs to verify correctness.

Assign points for each item on the Rubric, according to how well the candidate executed on that skill.

Add up all the points at the end, and record the total at the bottom of the page.

Example

insertion sort example

Documentation

Record detailed notes on the rubric, to share with the candidate when the interview is complete.

Solution

Algorithm Insertion sort creates a new array with all elements in the correct order. It does this by going item by item in the input array, and inserting it into an output array. The index in the output array is selected to keep the item in order. It is easiest to do this by going through the output array item by item, finding the first index "larger" than the item being inserted. Move all items after that index one higher, and set the inserting item to that index. This algorithm can be improved by using a binary sort on the output array to decide the insertion index.
PseudoCode
algorithm InsertionSort:
    declare array ARRAY <- input array values
    declare array OUTPUT <- empty array
    run a loop declaring I starting at 0 and incrementing up to length of ARRAY:
      INSERT into OUTPUT ARRAY value at index I
    return OUTPUT
algorithm INSERT:
  declare array ARRAY <- input array values
  declare number VALUE <- value to insert into ARRAY
  run a loop declaring I starting at 1 and incrementing up to length of ARRAY:
    if ARRAY at I compares less than VALUE:
      INSERT AT ARRAY index I VALUE
      return
  do INSERT AT ARRAY index 0 VALUE
algorithm INSERT AT:
  declare array ARRAY <- input array
  declare number INDEX <- index to insert at
  declare number VALUE <- value to insert
  run a loop declaring I starting a index length of ARRAY - 1 decrementing down to INDEX:
    set ARRAY at I to ARRAY at I - 1
  set ARRAY at INDEX to VALUE
Big O This algorithm is a straight forward iterate the input array / iterate the output array. Thus, is has `O(n)` time complexity for each item in the input array, and an `O(n)` complexity for looping the output array. Because this is nested, the overall runtime is `O(n^2)`. Note that INSERT loops from 0 to I, and INSERT AT loops from N down to I, so while INSERT AT is nested inside the INSERT loop, it only runs a single time & returns after. Because it creates an entire new output array, this is space `O(n)`. There are two runtime optimizations that could be done. INSERT can be implemented using binary search (because the array is sorted), and becomes `O(log n)`. INSERT AT can be implemented using a linked list, and can be `O(1)`. The first optimization is tricky to get right, and is an interview question itself. Doing both optimizations together gets the algorithm to a theoretical complexity of `O(n log n)`, but is very difficult to do correctly. There is one space optimization to be done. By tracking the start and end indexes of the arrays explicitly, the INSERT algorithm can "swap" from the right, unsorted portion of the array into the left, sorted portion. This makes the operation take only constant additional space for the indexes. But, like the runtime optimization, it can be tricky to keep track of all the indexes.