← Learning Hub
Advanced

Interview Patterns and an LRU Cache Project

Most interview questions are variations of a few patterns. Recognising the pattern - two pointers, sliding window, hashing, BFS, DP - is the key skill, and building an LRU cache combines several structures into one design.

Two pointers

Example
You can edit this code.

Sliding window

Example
You can edit this code.

Project: LRU cache

An LRU (Least Recently Used) cache keeps a fixed number of items and throws out the one used longest ago - used for product pages, images and API results. It's a famous interview question. A HashMap gives O(1) lookup; a doubly linked list keeps the usage order.

Example
You can edit this code.
In real Java code, LinkedHashMap with accessOrder = true and an overridden removeEldestEntry gives an LRU cache in a few lines - but interviewers usually want you to build it as above.

How to practise

  • Solve 2-3 problems a day on a practice site, starting with easy ones
  • For each problem: say the brute-force idea, its Big-O, then improve it
  • Group problems by pattern: hashing, two pointers, sliding window, BFS/DFS, DP
  • Explain your solution out loud - interviews test communication too
Key points
  • Two pointers and sliding window solve range problems in O(n).
  • LRU cache = HashMap + doubly linked list.
  • Practise by pattern and explain out loud.

📝 Test yourself

1. Two pointers on a sorted array: if the sum is too small, you...
2. Which two structures make an O(1) LRU cache?
3. Fill in the Java class that can act as an LRU cache with accessOrder = true:
<Integer, String>