Three easy problems the way a big tech product interview frames them: a count of shared friends, two notification streams merged without repeats, and the longest quiet stretch of a day.
A big tech product company builds apps that millions of people open every day: feeds, messages, search and photos. Its coding interviews use ordinary data structures, and the stories come from those apps. A warm-up problem might be a friend suggestion, a notification list or a quiet hour for a digest.
The interviewer listens for four habits.
The warm-up round has three easy problems. They look simple, and each hides one rule that the statement states once.
Exercise · Predict the output: Two streams, each id once. Stream A holds ids 11, 12, 13 and 14 at seconds 1, 4, 4 and 9. Stream B holds ids 21, 12, 22 and 14 at seconds 2, 4, 5 and 9. Equal seconds go to stream A. This merges the streams and shows each id once. What does it print?
times_a = [1, 4, 4, 9]
ids_a = [11, 12, 13, 14]
times_b = [2, 4, 5, 9]
ids_b = [21, 12, 22, 14]
i = 0
j = 0
sent = set()
shown = []
while i < len(times_a) or j < len(times_b):
if j == len(times_b) or (i < len(times_a) and times_a[i] <= times_b[j]):
note = ids_a[i]
i += 1
else:
note = ids_b[j]
j += 1
if note not in sent:
sent.add(note)
shown.append(note)
print(*shown)Each problem is one Academy idea. The new part is the rule in the statement.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| how many lists a person is on, outside the group | a count for each person in a hash map, with the group kept in a set | Frequency, palindromes and anagrams |
| two streams sorted by time, one list, each id once | a merge with the tie rule, and a seen-set | Merging sorted sequences and Seen-set and complement |
| the longest stretch with at most k events | a window over the seconds of the day, with a count for each second | Sliding window: fixed and variable windows |
For the friends, three members have lists 2, 4, 5, 6 and 1, 4, 5, 7 and 4, 6, 7, 3. The members are on each other's lists, and they are not suggestions. The outsiders are 4, 5, 6 and 7, and person 4 is on all three lists.
For the quiet period, the window moves over seconds and not over events. Take the events at seconds 1, 1, 1, 2, 5, 5, 6 and 9 in a day of 10 seconds. The longest quiet period is 2 seconds when k is 0, 3 when k is 1, 4 when k is 2 and 6 when k is 3.
Exercise · Predict the output: Friends outside the group. Members 1, 2 and 3 have the friend lists below. A person is suggested when they are on at least 1 list and are not a member. How many people are suggested?
owners = [1, 2, 3]
lists = [[2, 4, 5, 6], [1, 4, 5, 7], [4, 6, 7, 3]]
need = 1
group = set(owners)
seen = {}
for friends in lists:
for p in friends:
if p not in group:
seen[p] = seen.get(p, 0) + 1
count = 0
for p in seen:
if seen[p] >= need:
count += 1
print(count)The same notification can arrive on two streams, and a merge without a seen-set shows it twice.
The quiet period problem has a small trap in the window code. When the window holds seconds left to right, its length is right - left + 1.
Exercise · Find the bug: The length of the window. This returns the longest run of seconds with at most k events. The count for each second is in busy. It is right on busy days, but wrong when the whole run is quiet. Which line is wrong?
def longest_quiet_period(busy, k):
left = 0
inside = 0
best = 0
for right in range(len(busy)):
inside += busy[right]
while inside > k:
inside -= busy[left]
left += 1
best = max(best, right - left)
return bestThe quiet period window moves over the seconds of the day, and its length is right minus left plus one.
All three problems are easy, and together they make one warm-up round. Each time box is close to what an interviewer allows for an easy problem, with time to explain.
Solve them in this order. The friends need a count, the streams add a merge and a set, and the quiet period adds a window.
A warm-up problem rarely ends at the first answer. The interviewer changes one rule or asks about scale. Prepare one follow-up for each problem.
Treat the three problems as one interview of 60 minutes, with a timer. Work out loud, as if someone is listening.
Stop at each box. If a problem overruns, note why, and do the same problem again in a few days.
Checkpoint · Big tech product warm-up round
Q1/5 Two streams hold a notification at the same second, and the statement says stream A wins ties. Which one is shown first?
Answer: A
The statement states the tie rule, so follow it exactly. The same input then always gives the same output.
Q2/5 The same notification id arrives on both streams. How many times is it shown?
Answer: B
A seen-set remembers shown ids. The first copy is shown and later copies are skipped.
Q3/5 Members 1, 2 and 3 have friend lists. Person 2 is on member 1's list. Is person 2 a suggestion?
Answer: B
Members are not suggestions. Keeping the group in a set lets the count skip them in one step.
Q4/5 In the quiet period problem, what does the window move over?
Answer: B
Quiet is measured in consecutive seconds, so the window moves over seconds. A count for each second says how many events are inside.
Q5/5 The window holds seconds 3 to 6. What is its length?
Answer: B
Both ends are included, so the length is 6 - 3 + 1 = 4. Forgetting the plus one makes every answer one short.
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.