~/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.

Graphs

Cycle detection

Visited / on-stack sets; symlinks, CNAMEs, instruction loops.

Notes

Recognise it when: following pointers or links (CNAMEs, symlinks, instruction jumps, next pointers) that might loop.

  • Chain walk: keep a seen set for the current walk, and it's a cycle if you revisit.
  • Directed graph: DFS with three colours (white/grey/black). A grey node reached again means a cycle. Or run Kahn's and see whether nodes are left over.
  • Linked list, O(1) space: Floyd's tortoise and hare.
  • Many shared chains: memoize results so each node is resolved once (O(n) total).

Gotchas: "a -> a" self-loops, and chains into a cycle that doesn't include the start.

4 problems

Interview roadmap

Graphs Grids and networks: BFS, DFS, cycles, ordering.

Cycle detection guide

esc