KatInterview

Classic Data-Structure Drills: Cache, Trie, Heaps, Graphs

Data Structure DesignModel answer
MediumModel answerData Structure Designhash-tablestwo-pointerssliding-window

Last updated

Work through fifteen core algorithm patterns—LRU cache, trie, two-pointers, sliding window, topological sort, and more—naming invariants before coding.

This is a rapid-fire pattern drill rather than a single puzzle. You will be given a set of fifteen short problems covering the most common data-structure and algorithm archetypes. For each one you should:

  1. Identify the reusable pattern it belongs to (hashing, two pointers, sliding window, stack, heap, graph traversal, etc.).
  2. State the invariant the pattern maintains.
  3. Give a working solution with its time and space complexity.
  4. Call out the edge cases that would break a naive implementation.

The problems:

  1. Fixed-capacity recency cache. Implement get(key) and put(key, value), both O(1) average. When full, discard the item used least recently.

  2. Randomized set. Support insert(val), remove(val) and getRandom() (uniformly random element) in O(1) average time.

  3. Prefix dictionary. Support insert(word), search(word) (exact word) and startsWith(prefix).

  4. Top k frequent values. Given nums = [1,1,1,2,2,3] and k = 2, return the two most frequent values, e.g. [1,2] in any order.

  5. Product of all others. For [1,2,3,4] return [24,12,8,6] without using division, in O(n).

  6. Trapped rainwater. Given non-negative heights such as [0,1,0,2,1,0,1,3,2,1,2,1], compute the volume of water held between the bars.

  7. Largest container. Given vertical line heights, pick two lines maximizing the area they hold with the x-axis.

  8. Longest substring without repeats. For "abcabcbb" return 3.

  9. Group anagrams. Group ["eat","tea","tan","ate","nat","bat"] into letter-equivalent clusters.

  10. Decode nested repeats. Decode "3[a2[c]]" into "accaccacc".

  11. Count islands. Given a grid of land and water, count 4-connected land components.

  12. Course scheduling. Given n courses and prerequisite pairs, decide whether all can be completed (i.e., the prerequisite graph is acyclic).

  13. Merge k sorted lists. Merge lists like 1->4->5, 1->3->4, 2->6 into 1->1->2->3->4->4->5->6.

  14. In-place matrix rotation. Rotate an n×n matrix 90° clockwise without allocating another matrix.

  15. Longest mountain. In [2,1,4,7,3,2,5], the longest strictly-increasing-then-strictly-decreasing contiguous run is [1,4,7,3,2], length 5.

Hints

Hint 1

Fifteen problems, roughly eight patterns. Map each problem to a pattern before writing any code, then say the invariant out loud.

Hint 2

Several problems are secretly two-pointer scans (water trapping, container); the correctness argument always hinges on which side you can safely discard.

Hint 3

For O(1) random access plus O(1) removal, keep a dense array plus a value-to-index map and swap the victim with the last element before popping.

Hint 4

Carry the edge cases as a checklist: zeros in the product problem, capacity 0 in the cache, multi-digit repeat counts, empty grids and lists, plateaus in the mountain scan.