FastPrepNet Position Calculation from API Snapshots

Net Position Calculation from API Snapshots

Headlands Technologies logoHeadlands Technologies● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Calculate the final signed share quantity for each symbol from two supplied response tables. For this exercise, assume these arrays are immutable materialized responses from two read-only APIs; no live API calls or networking are required.

  • initialPositions contains rows [symbol, signedDecimalQuantity]. Initial symbols are unique. A symbol absent from this table starts at zero.
  • fills contains rows [symbol, side, positiveDecimalQuantity], with side exactly BUY or SELL. Each row is a separate valid execution delivered once, even when two rows have identical values.

A BUY adds its quantity and a SELL subtracts its quantity. Negative positions are allowed; a sell is never rejected for insufficient holdings.

Return [symbol, signedDecimalNetQuantity] for every symbol present in either table, including symbols whose result is zero. Sort rows by case-sensitive ASCII lexicographic symbol order. Quantities in the result use ordinary base10 notation without leading zeros or a plus sign; zero is "0". If both tables are empty, return an empty table.

Use signed 64-bit arithmetic for accumulated quantities. Do not modify either input table. Prices, open orders, profit/loss, pagination, retries, duplicate-event recovery and real external API implementation are outside this finite practice.

Function

calculateNetPositions(initialPositions: String[][], fills: String[][]) → String[][]

Examples

Example 1

initialPositions = [["A","10"],["B","-4"]]fills = [["A","BUY","3"],["A","SELL","5"],["C","SELL","2"]]return = [["A","8"],["B","-4"],["C","-2"]]

A finishes at10+3-5=8; B remains-4, and the previously absent C starts0 and becomes-2. Symbols are sorted.

Example 2

initialPositions = []fills = [["XYZ","BUY","5"],["XYZ","SELL","5"]]return = [["XYZ","0"]]

BUY and SELL cancel, but the encountered symbol must remain in the result with canonical zero.

Example 3

initialPositions = []fills = []return = []

Neither response contains a symbol, so return an empty table.

Constraints

  • 0 <= initialPositions.length + fills.length <= 1000.
  • Each symbol contains 1 through 10 ASCII letters A-Z or a-z.
  • Initial quantities are signed decimal integers with magnitude at most 10^9.
  • Fill quantities are positive decimal integers from 1 through 10^9.
  • All rows have the stated shape and valid values. Initial symbols are unique; execution rows are distinct events and are processed once.
  • Net quantities fit signed 64-bit integers; their magnitude is at most 10^12.

More Headlands Technologies problems

See Headlands Technologies hiring insights
public String[][] calculateNetPositions(String[][] initialPositions, String[][] fills) {
    // Write your code here.
}
initialPositions[["A","10"],["B","-4"]]
fills[["A","BUY","3"],["A","SELL","5"],["C","SELL","2"]]
expected[["A", "8"], ["B", "-4"], ["C", "-2"]]
Checking account…