Net Position Calculation from API Snapshots
Updated
Problem statement
Find the final number of shares held for each symbol. This number is signed, so it can be negative. The input is two tables. Treat them as saved responses from two read-only APIs. Do not change either table, and you do not need any live API or network calls.
initialPositionsholds the starting positions. Each row is[symbol, signedDecimalQuantity]: a symbol and its starting share count, written as a decimal integer that may be negative. Each symbol appears at most once in this table. A symbol that is not in this table starts at zero.fillsholds the executed trades. Each row is[symbol, side, positiveDecimalQuantity]. The side is exactlyBUYorSELL, and the quantity is a positive decimal integer. Every row is its own valid trade and is delivered once, even when two rows hold the same values.
A BUY adds its quantity to the symbol's position. A SELL subtracts its quantity. A position may go below zero. A sell is never refused because too few shares are held.
Return one row [symbol, signedDecimalNetQuantity] for every symbol that appears in either table. Keep symbols whose final position is zero. Sort the rows by symbol in ASCII order, comparing symbols character by character. Case matters: every uppercase letter comes before every lowercase letter. If one symbol is the start of a longer one, the shorter symbol comes first. Write each result quantity as a plain base 10 number, with no leading zeros and no plus sign. Write zero as "0". If both tables are empty, return an empty table.
Use signed 64-bit integers when you add and subtract. Prices, open orders, profit and loss, paging through results, retries, recovering from duplicate events and real API code are not part of this problem.
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 has one BUY and one SELL, so it ends at 10+3-5=8. B has no fills and stays at -4. C is not in initialPositions, so it starts at 0. Its SELL brings it to -2. Sorted by symbol, the rows come in the order A, B, C.
Example 2
initialPositions = []fills = [["XYZ","BUY","5"],["XYZ","SELL","5"]]return = [["XYZ","0"]]XYZ has one BUY and one SELL of the same size, so they cancel out and XYZ ends at zero. XYZ still appears in the input, so it keeps its row, with the quantity written as zero. The answer is that one row.
Example 3
initialPositions = []fills = []return = []Both tables are empty, so no symbol appears anywhere. The answer is an empty table.
Constraints
0 <= initialPositions.length + fills.length <= 1000- Each symbol contains 1 through 10 ASCII letters
A-Zora-z. - Initial quantities are signed decimal integers with magnitude at most
10^9. The magnitude is the value without its sign. - Fill quantities are positive decimal integers from
1through10^9. - All rows have the stated shape and valid values. Each symbol appears at most once in
initialPositions. Each row offillsis a separate trade and is counted once. - Every final quantity fits in a signed 64-bit integer, with magnitude at most
10^12.
More Headlands Technologies interview questions
- As-Of JoinOA · ONSITE INTERVIEW · Seen Aug 2026
- Triggered Order Event FilterONSITE INTERVIEW · Seen Oct 2024
- Best Time to Buy and Sell StockPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2024
- Best Time to Buy and Sell Stock IIONSITE INTERVIEW · Seen Apr 2024
- House RobberONSITE INTERVIEW · Seen Apr 2024
- Merge IntervalsONSITE INTERVIEW · Seen Apr 2024
- Disaster RecoveryOA · Seen Nov 2026
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Array Interval SumsOA · Seen Oct 2026
- Job Runner Chain ReportsOA · Seen Oct 2026
Similar questions at other companies
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Filter Anagrams of a TargetMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Rank Transform an ArrayAgoda · ONSITE INTERVIEWAgoda interview questions
- Deduplicate Logs: Keep FirstGoogle · ONSITE INTERVIEWGoogle interview questions
- Loyal Customers by Two-Day Product VarietyAmazon · ONSITE INTERVIEW
- Restaurant Vote LeadersZipRecruiter · PHONE SCREEN · ONSITE INTERVIEWZipRecruiter interview questions
- High-Severity Counts by Search TypeChainalysis · ONSITE INTERVIEWChainalysis interview questions
- Intersection of Two ListsJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- Pair Left and Right Socks by IDRoblox · ONSITE INTERVIEWRoblox interview questions