Interview Questions/Coding/Routing Prefix Dashboard

Routing Prefix Dashboard

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Routing Prefix Dashboard

A routing dashboard stores many lowercase service names. Analysts ask multiple prefix queries against the same catalog. For each requested prefix, determine how many catalog names begin with that exact prefix. The queries must be answered independently and returned in the same order in which they were requested. Input Format: You are given an array words containing catalog names and an array prefixes containing query prefixes. Output Format: Return an integer array where answer[i] is the number of words that start with prefixes[i].

Examples

Example 1
Input:
words = ["pay", "payroll", "paint", "pilot", "desk"]
prefixes = ["pa", "pay", "pi"]
Output:
3 2 1

Explanation: Prefix counts are pa:3, pay:2, pi:1. Therefore the dashboard prints 3 2 1.

Approach hint

The same catalog is queried many times.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp