← Google Interview Insights

Google·Machine Learning Engineer·Onsite - Coding / Algorithms·Senior

Senior
Jun 2026

Summary

Google ML Engineer interview, got a coding round with a data structures problem that had a twist I didn't fully see coming. The LRU variant with pinning caught me off guard in the implementation details even if the concept felt familiar.

Questions Asked (1)

Q1

Design and implement a modified LRU cache that supports get, put, pin, and unpin operations, where pinned keys cannot be evicted until explicitly unpinned. All operations should run in average O(1) time.

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

I knew LRU cold going in, doubly linked list plus a hashmap, done.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying requirements and constraints, then propose a design using a hash map for O(1) access and a doubly linked list for recency ordering, with a separate pinned list or flag to exclude pinned items from eviction. Discuss trade-offs, edge cases, and potential optimizations, and be prepared to outline code or pseudocode.

Pro tip: Demonstrate awareness of concurrency and memory overhead: mention that pinning can be implemented with a counter per key to handle multiple pins, and discuss thread-safety mechanisms like locks or lock-free structures if needed.

1. Clarify Requirements and Constraints

Ask about expected cache size, pin/unpin semantics (e.g., multiple pins per key), concurrency needs, and eviction policy details. Confirm that all operations must be average O(1).

2. Design Core Data Structures

Propose a hash map mapping keys to nodes, and a doubly linked list for recency order. For pinned items, either maintain a separate list or mark nodes as pinned and exclude them from eviction.

3. Define Operations and Eviction Logic

Detail get, put, pin, unpin: get moves node to front if not pinned; put adds/updates and evicts from tail if over capacity, skipping pinned nodes; pin increments a counter and moves node to pinned list; unpin decrements and moves back if zero.

4. Analyze Complexity and Edge Cases

Argue O(1) average time for all operations. Discuss edge cases: evicting when all items pinned, pinning non-existent key, unpinning unpinned key, and cache full of pinned items.

5. Discuss Trade-offs and Extensions

Compare separate pinned list vs. flag approach. Mention concurrency considerations, memory overhead, and possible optimizations like lazy eviction or using a single list with skip pointers.

Key Points to Mention

  • Hash map + doubly linked list for O(1) get/put and recency tracking.
  • Pinning mechanism: separate pinned list or per-key pin counter to handle multiple pins.
  • Eviction policy: evict least recently used unpinned item; if all pinned, either reject put or expand capacity.
  • Time complexity: all operations average O(1) due to hash map lookups and constant-time list manipulations.
  • Edge cases: pinning/unpinning non-existent keys, cache full of pinned items, and unpin when count reaches zero.
  • Concurrency and thread-safety: locks, read-write locks, or lock-free approaches for multi-threaded environments.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.