All practice tasks

Coding (Algo)

Ranked Prefix Index

Trie · Hard

A search service serves many prefix queries over a fixed catalog. Ranking is by popularity, with deterministic word ordering on ties.

Precompute an index returning the best three matching words for every query.

Input

Call

  • Implement solve(input) and return its result. Do not print from solve.

Input object

{ words:{text:string,score:integer}[], queries:string[] }

Input rules

  • Word texts are unique lowercase ASCII strings.

What each field means

  • words — unique lowercase word texts with integer popularity scores.
  • queries — prefixes to look up independently; an empty prefix matches every word.

Output

Return value and rules

  • Return one array per query, preserving query order.
  • Each array contains up to three matching texts, ranked by score descending and then text ascending.
  • Return an empty array for a query with no matching word.

Constraints

  • 0 <= words.length <= 20000; total word characters <= 200000
  • 0 <= queries.length <= 20000; total query characters <= 200000
  • Scores in [0,1000000]; word lengths are 1..80 and query lengths are 0..80. This also bounds the serialized suggestion output.

Examples

Example 1

{
  "queries": [
    "ap",
    "app",
    "x"
  ],
  "words": [
    {
      "text": "app",
      "score": 2
    },
    {
      "text": "ape",
      "score": 4
    },
    {
      "text": "apple",
      "score": 4
    }
  ]
}

Returns

[
  [
    "ape",
    "apple",
    "app"
  ],
  [
    "apple",
    "app"
  ],
  []
]

Example 2

{
  "queries": [
    ""
  ],
  "words": []
}

Returns

[
  []
]

Required complexity

O(N log N × L + W + Q) time and O(W) space; N is word count, L maximum word length, W total word characters and Q total query characters

What this task practices

Ranked Prefix Index is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as hard difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.