ORIGINAL CODING QUESTION
Autocomplete match totals
A course catalogue stores n course codes, each a sequence of tokens; duplicate codes are separate courses. For each of q search queries, count the courses whose code starts with the query tokens (a code equal to the query counts). Return the sum of these counts over all queries.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n codes, then q, then q queries. Each word is written as its length L followed by its L tokens. Queries use the same form.
Constraints
1 ≤ n, q ≤ 50; 1 ≤ L ≤ 20; every token is an integer in [1,26].
Example 1
Input
3 2 1 2 3 1 2 3 3 2 1 9 3 1 1 3 1 2 3 1 2
Output
4
Query (1) matches two codes, (1,2,3) one and (2) one, totalling 4.
Example 2
Input
2 2 5 5 2 5 5 2 3 5 5 5 1 6
Output
0
No code is long enough for (5,5,5), and nothing starts with 6.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Scanning every code for every query works for small inputs but repeats work.
Hint 2
Store in each trie node how many codes pass through it.
Hint 3
Walk each query down the trie; a missing branch contributes 0.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →