KatInterview

Replay a Typing Session Against a Ranked Suggestion History

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designhash-mapdesignstring-prefix

Last updated

Simulate keystroke-by-keystroke search suggestions over a frequency-weighted history that grows as queries are committed, ranking the top 3 matches by count and then text.

Replay a Typing Session Against a Ranked Suggestion History

A search box keeps a history of past queries, each with a count of how many times it was searched. As a user types one character at a time, the box suggests the best matching past queries. When the user finishes a query, that query is added to the history.

You are given the initial history and a recorded list of keystrokes. Replay the keystrokes in order and return the suggestions produced after each one.

Inputs

  • sentences: a list of strings, the initial history queries.
  • times: a list of integers, where times[i] is the count for sentences[i]. If the same string appears more than once in sentences, its counts are added together.
  • operations: a list of strings with the same length as arguments. Every entry must be "input". If any entry is something else, raise ValueError.
  • arguments: a list where arguments[i] is a one-element list [c] holding the character typed for the i-th operation.

Behavior of each keystroke

The replay keeps a buffer of the characters typed since the last '#'. It starts empty.

  • c is not '#': append c to the buffer. Collect every history query that starts with the whole buffer. Order them by count, highest first. Break ties by plain string comparison (ASCII order, smaller first). Output the first 3, or all of them if fewer than 3 match.
  • c is '#': the query is finished.
    • If the buffer is non-empty, add its text to the history. If the text is already in the history, increase its count by 1. Otherwise add it with count 1.
    • Clear the buffer.
    • Output an empty list [].

Each operation produces exactly one list in the result, in the same order as the operations.

Return value

A list of lists of strings, one inner list per operation.

Notes

  • A space ' ' is an ordinary character. In ASCII it sorts before every lowercase letter.
  • A '#' with an empty buffer changes nothing and outputs [].
  • A finished query that matched nothing while being typed is still added to the history with count 1.
  • A query finished with '#' counts as history only from the next keystroke onward, so it affects the following session's suggestions.
  • Once a prefix matches nothing, every further keystroke in that session outputs [] until the next '#'.

Constraints

  • 1 <= len(sentences) == len(times) <= 2000
  • 1 <= times[i] <= 100
  • 1 <= len(sentences[i]) <= 100
  • Each typed character is a lowercase letter 'a'-'z', a space ' ', or '#'.
  • len(operations) == len(arguments) <= 20000

Examples

Example 1

Input: replay_suggestions(['pin', 'pinterest', 'pinecone', 'pine'], [5, 3, 2, 2], ['input', 'input', 'input', 'input', 'input', 'input', 'input'], [['p'], ['i'], ['n'], ['e'], ['#'], ['p'], ['#']])

Output: [['pin', 'pinterest', 'pine'], ['pin', 'pinterest', 'pine'], ['pin', 'pinterest', 'pine'], ['pine', 'pinecone'], [], ['pin', 'pine', 'pinterest'], []]

Hints

Hint 1

Keep two things: a dictionary from query text to its count, and the buffer of characters typed so far in the current session.

Hint 2

Ranking is just a sort key of (-count, text). Take the first 3 after sorting the matches.

Hint 3

Counts only change when '#' is typed, so within one session the matches for a longer buffer are a subset of the matches for the shorter one. You can narrow a candidate list instead of rescanning the whole history.