Three medium problems the way a big tech product interview frames them: how many shortest introduction chains exist, the earliest finish of a build, and autocomplete with ties.
The warm-up round read the rule in the statement. The core round asks you to answer more than one question about the same data, and the rules get stricter. The problems are medium, and the stories are still everyday ones: introductions, builds and search boxes.
Interviewers look for three things in a core round.
Interviewers also like a plan stated early. Say what you will compute for each node, in what order, and what the last line returns.
Exercise · Predict the output: Ranking with ties. Words are ranked by higher count first, and alphabetically on equal counts. This lists the 3 best words that start with "ca". What does it print?
words = ["cat", "cart", "care", "car", "dog", "do"]
counts = [5, 2, 5, 5, 9, 9]
order = sorted(range(len(words)), key=lambda i: (-counts[i], words[i]))
top = []
for i in order:
if words[i].startswith("ca") and len(top) < 3:
top.append(words[i])
print(",".join(top))Each problem is one Academy idea with a stricter rule. Read the signal, then use the move.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| the fewest hops, and how many chains have that many | a breadth-first search that adds ways level by level | BFS and DFS on any graph |
| a module waits for its prerequisites, with unlimited machines | a topological order, with each start at the slowest prerequisite | Topological sort |
| suggestions for every typed prefix, best first | a trie whose nodes keep the best k words | Tries |
Take five people with friendships 0-1, 0-2, 0-3, 1-4, 2-4 and 3-4, and a request from 0 to 4. The fewest hops is 2, and there are 3 chains: through 1, through 2 and through 3. If person 2 has a private profile, the request can still end at 2 but cannot pass through it, so 2 chains remain. If persons 1, 2 and 3 are all private, no chain exists and the answer is -1 with 0 chains.
For the build, take modules with durations 3, 5, 1, 1 and 2. Module 0 comes first, modules 1 and 2 wait for it, module 3 waits for both, and module 4 waits for module 3. The earliest finish is 11 minutes, while the durations add up to 12, because modules 1 and 2 run at the same time.
Exercise · Predict the output: Chains through a private profile. Five people have friendships 0-1, 0-2, 0-3, 1-4, 2-4 and 3-4. Person 2 is private. This counts the shortest chains from 0 to 4. What does it print?
friends = [[1, 2, 3], [0, 4], [0, 4], [0, 4], [1, 2, 3]]
private = [0, 0, 1, 0, 0]
hops = [-1] * 5
ways = [0] * 5
hops[0] = 0
ways[0] = 1
queue = [0]
for person in queue:
if private[person] == 1 and person != 0:
continue
for f in friends[person]:
if hops[f] == -1:
hops[f] = hops[person] + 1
queue.append(f)
if hops[f] == hops[person] + 1:
ways[f] += ways[person]
print(ways[4])A breadth-first search adds the ways of each neighbour one hop closer, so the count builds level by level.
The build problem has a trap in how a module's start time is found. A module may wait for several prerequisites, and it starts when the slowest one ends.
Exercise · Find the bug: The slowest prerequisite. Each module has a lower number than every module that depends on it. This returns the earliest finish of the whole build. It is right when a module has one prerequisite, but wrong when it has two. Which line is wrong?
def earliest_finish(durations, before, after):
finish = [0] * len(durations)
for m in range(len(durations)):
start = 0
for j in range(len(before)):
if after[j] == m:
start = finish[before[j]]
finish[m] = start + durations[m]
return max(finish)A module starts when its slowest prerequisite finishes, so keep the largest finish and not the last one.
The three problems are medium. Each time box is what an interviewer allows for a medium problem, with time to explain and test.
Solve them in this order. The chains add a count to a search you know, the build adds durations to an order, and the autocomplete adds a structure with ranked lists.
A medium problem rarely ends at the first answer. Prepare one follow-up for each.
Treat the three problems as one session of 95 minutes, with a timer. A real core round often has two of them in 45 minutes, so the whole set is a hard rehearsal.
Stop each problem at its box, and note what was left.
Checkpoint · Big tech product core round
Q1/5 Two people are one hop closer to a target than a third person. Each has 1 shortest chain from the start. How many shortest chains reach the third person through them?
Answer: B
The third person gains the ways of each neighbour that is exactly one hop closer, so 1 + 1 = 2. Counting only the first neighbour would give 1.
Q2/5 A private person is in the middle of the only short chain. What happens to that chain?
Answer: B
A private person may end a chain but cannot be passed through. The search skips that person's neighbours.
Q3/5 A module waits for modules that finish at minutes 4 and 8. It takes 1 minute. When does it finish?
Answer: B
It starts when the slowest prerequisite ends, at minute 8, and finishes at minute 9. Adding both finishes would give 13, and taking the first one would give 5.
Q4/5 What does an earliest-finish build return when the dependencies contain a cycle?
Answer: B
Modules in a cycle wait for each other for ever, so some modules never run. The answer is -1.
Q5/5 Two words have the same count. Which comes first in the suggestions?
Answer: B
The rule is higher count first, and the alphabet on equal counts. Keeping the input order on ties would give an order that changes with the input.
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.