← Amazon Interview Insights

Amazon·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jul 2026Remote

Summary

Amazon SDE coding round, two problems back to back with complexity discussion after each. Nothing too wild but the follow-ups caught me a little off guard.

Questions Asked (5)

Q1

Design an LRU cache with get and put operations, both running in O(1) time.

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

I knew this one going in but still fumbled the eviction logic at first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying requirements and constraints, then propose a hash map combined with a doubly linked list to achieve O(1) get and put. Explain the design, walk through an example, and discuss trade-offs and edge cases.

Pro tip: Mention that you would use a sentinel head and tail to simplify edge cases, and discuss how the design can be extended for thread safety or persistence if needed.

1. Clarify Requirements

Ask about cache capacity, eviction policy (LRU), and whether operations need to be thread-safe. Confirm that get and put must be O(1).

2. Propose Data Structures

Suggest using a hash map for O(1) access to nodes and a doubly linked list to maintain recency order. Explain how they work together.

3. Detail Operations

Describe get: if key exists, move node to front and return value; else return -1. Describe put: if key exists, update value and move to front; else add new node and evict least recently used if capacity exceeded.

4. Walk Through Example

Trace through a sequence of operations to demonstrate correctness and O(1) time complexity.

5. Discuss Trade-offs and Edge Cases

Mention alternative implementations (e.g., OrderedDict in Python), handling of null values, and potential concurrency issues.

Key Points to Mention

  • Hash map provides O(1) lookup, doubly linked list provides O(1) insertion/deletion for recency updates.
  • Sentinel nodes (dummy head and tail) simplify boundary conditions.
  • Eviction policy: least recently used item is at the tail of the list.
  • Time complexity: both get and put are O(1) because all operations are constant time.
  • Space complexity: O(capacity) for storing up to capacity items.
  • Thread safety: can be addressed with locks or concurrent data structures, but may impact performance.

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

Q2

Given n nodes and a list of undirected edges, count the number of connected components in the graph.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Went with Union-Find because I'd just reviewed it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints (e.g., n up to 10^5, edges list format) and then present two standard solutions: Union-Find (Disjoint Set Union) and DFS/BFS. Compare their trade-offs in terms of time/space complexity, implementation simplicity, and suitability for dynamic graphs, then code the most appropriate one.

Pro tip: Mention that Union-Find with path compression and union by rank achieves near O(1) amortized time per operation, making it ideal for large graphs, and that it can handle dynamic edge additions, unlike DFS. Also, note that the number of components starts at n and decreases by 1 for each successful union.

1. Clarify the problem

Ask about constraints: size of n, number of edges, whether edges can be repeated or self-loops, and if the graph is guaranteed to be connected initially. Confirm the expected output (just the count).

2. Discuss approaches

Present two main approaches: Union-Find (DSU) and graph traversal (DFS/BFS). Explain how each works and their time/space complexities.

3. Compare trade-offs

Highlight that Union-Find is better for dynamic graphs and has near-constant time per operation, while DFS/BFS is simpler to implement and may be preferred for static graphs. Mention that both are O(n + e) time.

4. Implement the chosen solution

Write clean code for the selected approach, handling edge cases like empty graph or no edges. For Union-Find, include path compression and union by rank.

5. Test and verify

Walk through a small example (e.g., n=5, edges=[[0,1],[1,2],[3,4]]) to show the component count. Discuss potential pitfalls like 0-indexed vs 1-indexed nodes.

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank for near O(1) amortized operations.
  • DFS/BFS traversal to find connected components in O(n + e) time.
  • Time and space complexity analysis: both approaches are O(n + e) time, O(n) space.
  • Trade-offs: Union-Find supports dynamic edge additions; DFS/BFS is simpler for static graphs.
  • Edge cases: self-loops, duplicate edges, disconnected nodes, and 0-indexed vs 1-indexed nodes.
  • Initial component count = n, decrement on each successful union.

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

Q3

How would you make the LRU cache thread-safe, and where would lock contention become a bottleneck?

System DesignTechnical Trade-offs
Author's notes

Mentioned a global read-write lock and the interviewer immediately pushed on contention.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the need for thread safety in an LRU cache, then describe how to protect the shared data structures (hash map and doubly linked list) using locks. Discuss the trade-offs between coarse-grained and fine-grained locking, and identify where contention arises under high concurrency.

Pro tip: Mention that Amazon often deals with high-throughput systems, so you should emphasize minimizing lock contention and consider lock-free or read-optimized approaches like read-write locks or sharding.

1. Identify shared state

Point out that the hash map and doubly linked list are shared mutable state that must be protected from concurrent access.

2. Choose a locking strategy

Discuss options: a single global lock (simple but high contention), fine-grained locks per bucket or node, or read-write locks to allow concurrent reads.

3. Analyze contention points

Explain that the head and tail of the linked list are frequently accessed during get and put operations, causing contention, especially with a global lock.

4. Propose optimizations

Suggest sharding the cache into multiple independent segments, each with its own lock, or using lock-free data structures with atomic operations.

5. Evaluate trade-offs

Compare complexity, scalability, and correctness; note that sharding reduces contention but complicates eviction and may lead to uneven load.

Key Points to Mention

  • Coarse-grained vs fine-grained locking
  • Read-write locks for read-heavy workloads
  • Lock contention at the head/tail of the LRU list
  • Sharding the cache to reduce contention
  • Lock-free approaches using atomic operations (e.g., CAS)
  • Performance implications under high concurrency

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

Q4

How would you extend the LRU cache to use a least-frequently-used eviction policy while keeping all operations O(1)?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Knew this was hard.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying that LRU evicts based on recency while LFU evicts based on access frequency, and that maintaining O(1) for LFU requires careful data structure design. Then propose a solution using a combination of a frequency map, a doubly linked list per frequency, and a min-frequency pointer, explaining how get and put operations update frequencies in constant time.

Pro tip: Mention that LFU can suffer from cache pollution where historically frequent items block new items; briefly discuss aging or frequency decay as a practical enhancement, showing you think beyond textbook algorithms.

1. Clarify requirements and constraints

Confirm that the cache has a fixed capacity, operations are get(key) and put(key, value), and both must be O(1). Note that ties in frequency can be broken by LRU order for determinism.

2. Design core data structures

Use a hash map for key-to-node lookup, a frequency map mapping frequency to a doubly linked list of nodes with that frequency, and a min_freq variable to track the lowest frequency for O(1) eviction.

3. Implement get operation

On get, retrieve the node, increment its frequency, move it from its current frequency list to the next frequency list, and update min_freq if the old list becomes empty.

4. Implement put operation

On put, if key exists, update value and increment frequency like get. If new, insert with frequency 1; if at capacity, evict the least frequently used node (from min_freq list, LRU order for ties) before inserting.

5. Analyze complexity and edge cases

Explain that all operations are O(1) due to hash map lookups and constant-time list manipulations. Discuss edge cases like capacity 1, updating existing keys, and tie-breaking.

Key Points to Mention

  • Difference between LRU and LFU eviction policies
  • Use of a frequency map (frequency -> doubly linked list of nodes)
  • Maintaining a min_freq pointer for O(1) eviction
  • Incrementing frequency on access and moving nodes between lists
  • Tie-breaking by LRU order within the same frequency
  • Handling cache pollution and potential frequency aging

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

Q5

If graph edges arrive as a stream rather than all at once, which approach scales better between DFS and Union-Find?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Union-Find is the obvious answer here and I said so.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Compare DFS and Union-Find for streaming graph edges, focusing on dynamic connectivity and scalability. Highlight that Union-Find is better suited for incremental edge additions due to near-constant time per operation, while DFS requires reprocessing. Discuss trade-offs and mention Amazon's leadership principles like customer obsession and deliver results.

Pro tip: Mention that Union-Find with path compression and union by rank achieves amortized O(α(n)) per operation, making it ideal for large-scale streaming scenarios, and relate it to real-world systems like network monitoring or social network analysis.

1. Clarify the problem

Restate the question to ensure understanding: we need to determine which algorithm scales better when edges arrive as a stream, requiring dynamic connectivity queries.

2. Analyze DFS for streaming

Explain that DFS is typically used for static graphs; for each new edge, it would require re-running DFS from scratch, leading to O(V+E) per edge, which is inefficient for large streams.

3. Analyze Union-Find for streaming

Describe Union-Find's ability to incrementally add edges and answer connectivity queries in nearly constant amortized time, making it highly scalable for streaming scenarios.

4. Compare and conclude

Summarize that Union-Find scales better due to its incremental nature and efficient operations, while DFS is impractical for dynamic updates.

5. Relate to Amazon context

Tie the answer to Amazon's leadership principles, such as customer obsession (efficient systems) and deliver results (scalable solutions), and mention potential use cases like real-time network analysis.

Key Points to Mention

  • Union-Find with path compression and union by rank achieves amortized O(α(n)) per operation.
  • DFS requires O(V+E) time per edge addition if re-run, which is inefficient for streams.
  • Union-Find supports incremental edge additions and connectivity queries efficiently.
  • DFS is better for static graphs or when full traversal is needed.
  • Scalability considerations: memory usage, time complexity, and dynamic updates.
  • Real-world applications: social networks, network connectivity, and clustering.

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