~/problems

Problems

Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.

Linked lists

Linked lists

Dummy heads, pointer rewiring, fast/slow pointers.

Notes

Recognise it when: you're given ListNodes. Expect pointer rewiring with O(1) extra space.

def reverse(head):
    prev = None
    while head:
        head.next, prev, head = prev, head, head.next
    return prev
  • A dummy head (dummy = ListNode(0, head)) removes the special cases for the first node.
  • Fast/slow pointers: the middle (slow ends there), cycle detection (they meet), the cycle start (reset one to head and step both by 1), and the nth from the end (move fast n ahead first).
  • Merge two sorted lists with a tail pointer.

Gotchas: save next before you rewire. Check fast and fast.next. Draw it.

13 problems

Interview roadmap

Linked lists Reverse, merge, detect cycles, build an LRU cache.

Linked lists guide

esc