FastPrepDynamic Prefix Query Counts

Dynamic Prefix Query Counts

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Problem statement

Maintain a collection of search queries and answer how many stored query occurrences begin with a given prefix. Duplicate queries count separately.

The collection is initially empty. You are given arrays operations and values of equal length. Process entry i in order:

  • insert: add one occurrence of the query values[i].
  • delete: remove one occurrence of the exact query values[i]. If it is absent, leave the collection unchanged.
  • count: count all stored query occurrences that start with the prefix values[i].

Return only the results of the count operations, in their original order. Mutations do not produce output. An empty prefix matches every stored query occurrence. If there are no count operations, return an empty integer array.

The problem statement continues
Pro

Examples

Example 1

operations = ["insert","insert","insert","count","delete","count","count"]values = ["app","apple","app","app","app","app",""]return = [3,2,2]

The first prefix count includes both copies of app and apple. Deleting one app leaves two matching occurrences. The empty prefix then counts the same two stored occurrences.

FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
See Google hiring insights
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week