Interview 01

Max stack. Write a method that returns the “biggest” element in a stack.

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.

Documentation

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

Solution

Algorithm In order to get the largest value in a stack we can read every value in stack and track the largest number as we iterate. Using a variable to represent the largest value so far, we can use a loop iterate through our stack, comparing every value to the largest value found thus far. After all values are read and compared, we can return our variable for the largest value.
PseudoCode
algorithm MAX_STACK:
  declare stack STACK <- input
  declare number LARGEST <- null for now
  declare node CURRENT <- STACK.pop()
  while CURRENT is not null:
    if the CURRENT value is greater than the LARGEST value
        set LARGEST value to the CURRENT value
    set CURRENT to whatever is popped off the STACK
  return LARGEST
Big O This solution has a complexity of 0(n) for time and 0(1) for space. Our code will run longer with linear growth, the more values are in the input Stack the more values we have to read and compare. Our space is constant, we are only storing a single number and updating that single value whenever we find a value that is larger.