FastPrepNet Position Calculation from API Snapshots
Problems/Net Position Calculation from API Snapshots

Net Position Calculation from API Snapshots

Headlands Technologies logoHeadlands Technologies● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Headlands Technologies onsite interview. The report asks you to compute a position using several outside APIs. It does not define those APIs or say what position means. Here you are given the starting position and the execution responses. Net shares are the starting position plus BUY minus SELL. We chose the response format, that each execution counts exactly once, how short and zero balances work, the order, the integer size and the limits. No real provider interface is copied. Our version is about 45% the same as the reported question.

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.

  • initialPositions holds 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.
  • fills holds the executed trades. Each row is [symbol, side, positiveDecimalQuantity]. The side is exactly BUY or SELL, 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-Z or a-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 1 through 10^9.
  • All rows have the stated shape and valid values. Each symbol appears at most once in initialPositions. Each row of fills is 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
All Headlands Technologies interview questionsSee Headlands Technologies hiring insights
Saved
class Solution {
    public String[][] calculateNetPositions(String[][] initialPositions, String[][] fills) {
        // Write your code here.
    }
}
Checking account…