Order Sequence Prefix Counts and Quantities
Updated
Problem statement
Keep active orders in the order they were added. Each order has a case-sensitive string ID and a positive quantity. Process operations from first to last:
PUT id quantityappends a new order at the end. Every PUT uses an ID that has never appeared in an earlier PUT.DELETE idremoves the complete active order.MODIFY id quantityreplaces the active order's quantity and preserves its position.QUERY idreturns the number of active orders strictly before the named order and their total quantity.
Every DELETE, MODIFY and QUERY names an active order. Order follows when each PUT occurred, not the numeric or lexicographic order of IDs. The queried order itself is excluded. Return one two-element row [count,totalQuantity] per QUERY, in request order; other commands produce no result rows.
No command may scan the whole sequence. You may do one O(Q log Q) preparation step before running the commands, where Q is the number of operations. After that, every command must take O(log Q) time in the worst case. IDs have a limited length, so comparing two IDs takes a limited amount of work.
Function
processOrderSequence(operations: String[]) → long[][]Examples
Example 1
operations = ["PUT 1 1","PUT 2 2","PUT 3 3","PUT 4 4","DELETE 2","MODIFY 3 1","QUERY 4"]return = [[2,2]]After deleting order 2, the active sequence is 1,3,4. Orders 1 and 3 are strictly before 4 and have quantities 1 and 1, giving count 2 and sum 2.
Example 2
operations = ["PUT z 7","PUT A 9","QUERY z","QUERY A","PUT a 4","DELETE A","MODIFY z 10","QUERY a"]return = [[0,0],[1,7],[1,10]]Insertion order, not the spelling of an ID, controls the prefix. Initially z comes before A. After deleting A and changing z to 10, only z precedes a.
Example 3
operations = []return = []No query occurs in the empty batch, so no result rows are returned.
Constraints
0 <= operations.length <= 10^5.- Each ID contains 1 to 40 ASCII letters or digits and is case-sensitive.
1 <= quantity <= 10^9.- Commands use the exact uppercase forms shown with single spaces between tokens.
- PUT IDs are globally unique. DELETE, MODIFY and QUERY always name an active order.
- Use signed 64-bit totals; an aggregate may reach
10^14.
More Headlands Technologies Phone Screen questions
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Market Order BookPHONE SCREEN · Seen Jun 2026
- Gradient Descent on a Sinusoidal ObjectivePHONE SCREEN · Seen Apr 2026
- Pearson Correlation CoefficientPHONE SCREEN · Seen Apr 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Apr 2026
- Queue Mean from Corrective SnapshotsPHONE SCREEN · Seen Mar 2026
- Queue Mean QueriesPHONE SCREEN · Seen Mar 2026
- CPU Register Bitwise OperationsPHONE SCREEN · Seen Nov 2025
- Merge K Sorted SequencesPHONE SCREEN · Seen Oct 2024
- Top Trade Events by NotionalPHONE SCREEN · Seen May 2024
Similar questions at other companies
- Employee Ratings Management SystemAmazon · PHONE SCREEN
- Running Medians and PercentilesStackAdapt · PHONE SCREENStackAdapt interview questions
- Range ModuleWalmart · PHONE SCREENWalmart interview questions
- Dynamic K-th Largest QueriesGoogle · ONSITE INTERVIEWGoogle interview questions
- Delivery Cost Tracker with Effective-Dated RatesRippling · PHONE SCREENRippling interview questions
- Versioned Friend RecommendationsOpenAI · PHONE SCREENOpenAI interview questions
- Dynamic Fixed-K Score LeaderboardAnchorage · PHONE SCREEN
- Dynamic First and Last Position in Sorted DataBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Snapshot Map with Sparse Version HistorySnorkel AI · PHONE SCREEN
- Top K Pins with Insert and QueryPinterest · PHONE SCREENPinterest interview questions