FastPrepOrder Sequence Prefix Counts and Quantities

Order Sequence Prefix Counts and Quantities

Headlands Technologies logoHeadlands Technologies● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Maintain an insertion-ordered sequence of active orders. Each order has a case-sensitive string ID and a positive quantity. Process the finite operations batch in order:

  • PUT id quantity appends a new order at the end. Every PUT uses an ID that has never appeared in an earlier PUT.
  • DELETE id removes the complete active order.
  • MODIFY id quantity replaces the active order's quantity and preserves its position.
  • QUERY id returns the number of active orders strictly before the named order and their total quantity.

Every DELETE, MODIFY and QUERY names an active order. Ordering follows PUT chronology, not numeric or lexicographic ID order. 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 applied command may scan the entire sequence. In this finite batch adapter, one O(Q log Q) preprocessing step is allowed before applying the commands, where Q is the number of operations. After preprocessing, support every applied operation in worst-case O(log Q) time. ID lengths are bounded below, so comparing IDs takes bounded 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 order2, the active sequence is1,3,4. Orders1 and3 are strictly before4 and have quantities1 and1, giving count2 and sum2.

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 to10, 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 problems

See Headlands Technologies hiring insights
public long[][] processOrderSequence(String[] operations) {
    // Write your code here.
}
operations["PUT 1 1","PUT 2 2","PUT 3 3","PUT 4 4","DELETE 2","MODIFY 3 1","QUERY 4"]
expected[[2,2]]
Checking account…