Interview 01

Reverse a Linked List.

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

Input Output
head->{3}->{2}->{1} head->{1}->{2}->{3}
head->{'a'}->{'b'}->{'c'} head->{'c'}->{'b'}->{'a'}

Documentation

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

Solution

Algorithm Reversing a linked list requires you to read every node value in the list and move the values from each next Node, to the previous Node. If we have a singly linked list this means that we should store previous nodes somewhere so we can read the values as we move through our linked list. While we iterate through the list, we swap our next nodes value with the previous nodes value until we reach the end of the linked list.
PseudoCode
algorithm REVERSE_LINKED_LIST:
  declare LinkedList LL <- input
  declare Node PREVIOUS <- set to null for now
  declare Node CURRENT <- set to head of LL
  while CURRENT Node is not null:
    declare Node NEXT <- The next Node attached to CURRENT
    set the next node on CURRENT node <- PREVIOUS node
    set PREVIOUS Node <- CURRENT node
    set CURRENT Node <- NEXT node
  set LL.head <- PREVIOUS node
  return LL
Big O This solution has a complexity of 0(n) for time, and a constant 0(1) complexity for space. Since we have to read every value in our linked list in order to reverse it, we should be performing as many operations as our input size (n). But since we are only reassigning values and not storing them in another structure, our space complexity does not grow with the inputs and remains constant throughout our functions run time.