Three hard problems the way a big tech product interview frames them: trending topics in a sliding window, a feed cache as a design round, and a median of response times over time.
A stretch round has one hard problem. For a big tech product company the story is usually a feed, a cache or a monitor, and the hard part is state that changes as time passes. Counts rise as posts arrive and fall as posts get old.
A cache fills up and must forget. A window slides and drops its oldest entries.
Three habits make these problems manageable.
One problem in this set is a design round. You receive a list of commands, such as put, get and drop, and answer each in order.
Exercise · Predict the output: One put, several evictions. A cache holds up to 10 units. Its pages have sizes 3, 5 and 6, with the least recently used first, and the total is 14. Pages are removed from the front until the total fits. How many pages are removed?
capacity = 10
sizes = [3, 5, 6]
total = 0
for s in sizes:
total += s
removed = 0
head = 0
while total > capacity:
total -= sizes[head]
head += 1
removed += 1
print(removed)Each problem is an Academy idea with a twist for state that changes. Read the signal, then use the move.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| the k most frequent topics now, counts rise and fall | a heap of entries, with lazy deletion of out-of-date ones | Heaps and priority queues |
| a cache measured in total size, least recently used first | a map and a doubly linked list, with a loop for eviction | Doubly linked lists and the LRU cache |
| the middle value of a window that slides | two heaps, one for each half, with lazy removal | Two heaps and running medians |
Take posts at seconds 1, 2, 3, 4, 6, 7 and 8 with topics 5, 5, 9, 9, 9, 5 and 7, and a window of 5 seconds. At second 4 the counts are 2 for topic 5 and 2 for topic 9. The tie goes to the smaller id, so the top 2 are 5 and 9.
At second 8 topic 9 has 2 posts and topics 5 and 7 have 1 each, so the top 2 are 9 and 5. At second 10 every topic has 1, and the top 2 are 5 and 7.
For the cache, take capacity 10 and these commands, in order.
The answers are 0, 0, 1, 20, 2, -1, 0 and -1. The third put removes 1 page, and the fifth command removes 2.
Exercise · Predict the output: Skipping an out-of-date entry. Topic 7 once had 3 posts and now has 1. Topic 9 has 2. The entries are (negative count, topic) pairs, best first. This picks the top 2 topics and skips entries that are out of date. What does it print?
counts = [0] * 10
counts[7] = 1
counts[9] = 2
entries = sorted([(-3, 7), (-2, 9), (-1, 7)])
chosen = []
for neg, topic in entries:
if len(chosen) == 2:
break
if counts[topic] == -neg and topic not in chosen:
chosen.append(topic)
print(*chosen)A cache limited by total size removes least recently used pages until the total fits, so one put can remove several.
The window boundary is the usual trap in trending topics. A post counts when q - window < t <= q, so a post that is exactly window seconds old has already left.
Exercise · Find the bug: The post that is exactly old enough. This counts the posts in the window at time q: a post counts when it is less than window seconds old. It is right except at one boundary. Which line is wrong?
def posts_in_window(times, q, window):
count = 0
for t in times:
if t <= q and t >= q - window:
count += 1
return countThe median problem needs removal from the middle of a heap, which a heap cannot do. The usual answer is to mark the value as gone and fix the sizes of the halves straight away.
A heap entry can go out of date, so compare it with the live count at the top and skip it when they differ.
All three are hard, and one is a design round. Each time box is what a strong candidate needs. A real loop would give you one of them.
Solve them in this order. The trending topics add lazy deletion to a heap, and the cache adds a size limit to a list you know.
The median needs lazy removal in two heaps at once.
A stretch problem is followed by a harder question. Prepare one for each.
The full set is 140 minutes, which is longer than any one interview. Use it as three sessions, one problem each, with a timer.
For the design round, say aloud what each command reads and changes. A get changes the order of use, and a drop changes the total.
Checkpoint · Big tech product stretch round
Q1/5 A heap holds an entry for topic 7 with a count of 3, but topic 7 now has 1 post. What should the code do when that entry reaches the top?
Answer: B
A heap cannot change an entry in the middle. The code compares the entry with the live count and skips the entry when they differ.
Q2/5 A cache is limited to 10 units and holds 8. A page of 6 units is put. Pages of 3 and 5 units are the oldest. How many are removed?
Answer: B
The total becomes 14. Removing the 3-unit page leaves 11, which is still over 10, so the 5-unit page goes too.
Q3/5 Why does a get change a feed cache?
Answer: B
Reading counts as using. The page moves to the front of the list, so it is the last to be evicted.
Q4/5 A post arrives at second 5, and the window is 5 seconds. Is it in the window at second 10?
Answer: B
A post counts while q - window < t. At second 10 the post has an age of 5, which is not below the window, so it has left.
Q5/5 In a sliding median with two heaps, a value leaves the window but stays inside a heap. What happens to the sizes of the halves?
Answer: A
The sizes decide when to move a value between halves, so they must describe the live values. The stale value is removed from the heap later, when it reaches the top.
Review cards from this lesson.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.