FastPrepLRU Cache for Query Results
Problems/LRU Cache for Query Results

LRU Cache for Query Results

Amazon logoAmazon● MediumNEW GRADFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionExamples

Problem statement

Source note: Reported in Amazon phone screens for new grad and full-time roles. The reports describe a cache for database query results with an LRU rule. They also discuss lookup cost, concurrency, and heavy scan traffic. Later reports mention the exact LRU Cache problem, but their extra follow-ups were not kept. Our version is a finite, ordered list of operations that uses the standard LRU get, put, recency, and eviction rules. Concurrency and scan resistance are topics to talk about, and we do not check them. Our version is about 96% the same as the reported questions.

Maintain a cache with positive integer capacity. Process each operation atomically in the supplied completed serialization order:

  • [1, key] performs get(key). Return the stored value, or -1 when the key is absent. A successful get makes the key most recently used.
  • [2, key, value] performs put(key, value). Insert or update the key and make it most recently used. Updating an existing key does not change the cache size. If an insertion exceeds capacity, evict exactly the least recently used key.

Return one string per operation: the decimal result of each get and the literal string "null" for each put. Implement both operations in O(1) expected time.

The problem statement continues
Pro

Examples

Example 1

capacity = 2operations = [[2,1,10],[2,2,20],[1,1],[2,3,30],[1,2],[1,3]]return = ["null","null","10","null","-1","30"]

Reading key 1 makes it recent, so inserting key 3 evicts key 2.

02 · Example 2
Original screenshot from the interview report
Pro
FastPrep Pro
Reported in 1 Amazon interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 1 more worked example, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week

More Amazon Phone Screen questions

  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
  • Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
  • Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
  • Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 3SumPHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
  • LRU Key-Value Cache OperationsCohesity · PHONE SCREEN
  • Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
  • LRU CacheAMD · ONSITE INTERVIEWAMD interview questions
  • LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • Least Recently Used CacheByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
  • LRU CacheCoupang · PHONE SCREEN
  • LRU Cache with Hit and Miss CountersLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Least Recently Used CacheGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • LRU CacheBlinkit · PHONE SCREENBlinkit interview questions

Interview reports that mention this question

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon Experienced Hire OA and Interviews: 21 Experiences
  • Amazon Full-Time Interview Timelines: 9 Experiences (Part 2 of 2)
  • Amazon Full-Time SDE Interviews: 9 Experiences (Part 1 of 2)
  • Amazon Frontend Spring 2025 Loops: 11 Experiences (Part 2 of 2)
  • Amazon Frontend Engineer Interviews: 21 Experiences (Part 1 of 2)
See Amazon hiring insights
CodePython 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import heapq
from collections import defaultdict
 
class Solution:
def solve(self, values, queries):
# Build the adjacency structure
n = len(values)
graph = defaultdict(list)
for u, v in queries:
graph[u].append(v)
 
best = [float("inf")] * n
heap = [(0, 0)]
while heap:
cost, node = heapq.heappop(heap)
if cost > best[node]:
continue
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Amazon interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 1 more worked example, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week