Linked List Interview Prep
Spencer Moran
11/20/13
Why Linked Lists?
• Simplicity
• Little variation
• Pointers and references
Singly Linked List

Almost all LL interview questions are based on singly linked lists
Doubly Linked List
Implementation
• Can be implemented as
an ADT
• Often use templates
• List is referred to by the
head, the first node in
the list
• If you modify the
list, you must return the
head of the new list
Traversal
Insertion
Deletion
Linked List Question Strategies
• Use two (or more??) pointers to traverse the
list
• Traverse pointers at different speeds
• Use an additional data structure
Common Linked List Questions

Find the kth to last element in a singly linked list.
Strategy
• Use multiple pointers to traverse the list.
• Return an error if the list is not at least k
nodes long.
• Traverse the list with 2 pointers:
– Pointer1 starts at head
– Pointer2 starts k nodes in front of the head.
– When pointer2 reaches the end of the list, return
pointer1
Common Linked List Questions
Determine if a singly linked list has a loop or cycle.
Strategy
• Use multiple pointers and traverse them at
different speeds.
• Start both pointers at head:
– Traverse the list, moving fast pointer twice as fast
as the slow pointer.
– If fast pointer reaches the end of the list, there is
no cycle.
– If the pointers ever are equal to one
another, there is a cycle.
BONUS QUESTION
Determine if a linked list has a cycle. If it
does, return the element where the cycle
begins.
Bonus Question #2

Find the length of the cycle without traversing it
multiple times.
Common Linked List Questions

Reverse a singly linked list.
Strategy
• Use an additional data structure.
• Traverse the list, pushing each node into the
stack.
• Set the new head equal to the first element in
the list.
• Starting at the new head, add the top element
from the stack until the stack is empty.
Other common questions
• Remove duplicates from a singly linked list
• Merge sort
• Remove element from middle of list (without
head)
• Check if the linked list is a palindrome
• Fold a singly linked list using no containers
Meeting Summary
We also covered a bunch of questions at the end
of the meeting. These questions were mostly
about linked lists, but we also started covering
trees and decided that trees and graphs will be
our next topic. The date of our next meeting will
be arranged through the facebook group.

Linked lists

  • 1.
    Linked List InterviewPrep Spencer Moran 11/20/13
  • 2.
    Why Linked Lists? •Simplicity • Little variation • Pointers and references
  • 3.
    Singly Linked List Almostall LL interview questions are based on singly linked lists
  • 4.
  • 5.
    Implementation • Can beimplemented as an ADT • Often use templates • List is referred to by the head, the first node in the list • If you modify the list, you must return the head of the new list
  • 6.
  • 7.
  • 8.
  • 9.
    Linked List QuestionStrategies • Use two (or more??) pointers to traverse the list • Traverse pointers at different speeds • Use an additional data structure
  • 10.
    Common Linked ListQuestions Find the kth to last element in a singly linked list.
  • 11.
    Strategy • Use multiplepointers to traverse the list. • Return an error if the list is not at least k nodes long. • Traverse the list with 2 pointers: – Pointer1 starts at head – Pointer2 starts k nodes in front of the head. – When pointer2 reaches the end of the list, return pointer1
  • 13.
    Common Linked ListQuestions Determine if a singly linked list has a loop or cycle.
  • 14.
    Strategy • Use multiplepointers and traverse them at different speeds. • Start both pointers at head: – Traverse the list, moving fast pointer twice as fast as the slow pointer. – If fast pointer reaches the end of the list, there is no cycle. – If the pointers ever are equal to one another, there is a cycle.
  • 16.
    BONUS QUESTION Determine ifa linked list has a cycle. If it does, return the element where the cycle begins.
  • 18.
    Bonus Question #2 Findthe length of the cycle without traversing it multiple times.
  • 19.
    Common Linked ListQuestions Reverse a singly linked list.
  • 20.
    Strategy • Use anadditional data structure. • Traverse the list, pushing each node into the stack. • Set the new head equal to the first element in the list. • Starting at the new head, add the top element from the stack until the stack is empty.
  • 22.
    Other common questions •Remove duplicates from a singly linked list • Merge sort • Remove element from middle of list (without head) • Check if the linked list is a palindrome • Fold a singly linked list using no containers
  • 23.
    Meeting Summary We alsocovered a bunch of questions at the end of the meeting. These questions were mostly about linked lists, but we also started covering trees and decided that trees and graphs will be our next topic. The date of our next meeting will be arranged through the facebook group.

Editor's Notes

  • #5 Each node has a pointer to the next node and the previous node. Not seen in interviews because they trivialize many problems