Stock Ledger Reconciliation

Hard
0/120
0 upvote
Asked in company
Addepar

Problem statement

You are an auditor for a financial firm, tasked with reconciling stock inventories from three different ledgers: a starting ledger, a transactions ledger, and a final ledger. Each ledger is represented as a list of strings.


start list: Contains the initial count of each stock at the beginning of the day. Format: "{stock_name}:{count}".


transactions list: Contains all the buy (+) or sell (-) transactions that occurred during the day. Format: "{stock_name}:{change}". A positive change is a buy, a negative change is a sell.


final list: Contains the expected final count of each stock at the end of the day. Format: "{stock_name}:{count}".


Your task is to calculate the actual final stock counts by applying the transactions to the starting counts and then compare this result with the final ledger. You must generate a "reconciliation report" that lists all stocks with discrepancies.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer N1, the number of records in the start list.

The next N1 lines contain the records for the start list.

The next line contains an integer N2, for the transactions list.

The next N2 lines contain the records for the transactions list.

The next line contains an integer N3, for the final list.

The next N3 lines contain the records for the final list.


Output Format:
For each stock that has a discrepancy between your calculated final count and the final ledger's count, print a line in the format: "{stock_name}:{difference}".

The difference is calculated as (calculated_final_count - final_ledger_count). A positive difference means the calculated amount was higher; a negative difference means it was lower.

If a stock exists in one final calculation but not the other, it is also a discrepancy.

The output lines must be sorted alphabetically by stock name.

If there are no discrepancies, print No discrepancies found.


Note:
A stock might appear multiple times in any list (e.g., multiple transactions for the same stock). These should be aggregated.

A hash map (or dictionary) is the ideal data structure to store the aggregated counts for each stock.
Sample Input 1:
3
APPL:10
GOOG:15
MSFT:5
3
APPL:5
GOOG:-5
TSLA:10
3
APPL:15
GOOG:10
MSFT:5


Sample Output 1:
TSLA:10


Explanation for Sample 1:
1.  Calculated Final State:
- Start with initial counts: `{APPL: 10, GOOG: 15, MSFT: 5}`.
- Apply transactions: `APPL` becomes `10+5=15`, `GOOG` becomes `15-5=10`, `TSLA` is added with `10`.
- The calculated final counts are: `{APPL: 15, GOOG: 10, MSFT: 5, TSLA: 10}`.
2.  Given Final State: The final ledger is `{APPL: 15, GOOG: 10, MSFT: 5}`.
3.  Comparison:
- APPL, GOOG, and MSFT match.
- TSLA has a calculated value of 10 but is missing from the final ledger (implicitly 0). The difference is `10 - 0 = 10`.


Sample Input 2:
2
V:100
SPY:200
2
V:-20
SPY:30
2
V:80
SPY:230


Sample Output 2:
No discrepancies found


Explanation for Sample 2:
- Calculated `V` = `100 - 20 = 80`. Final ledger `V` = `80`. (Match)
- Calculated `SPY` = `200 + 30 = 230`. Final ledger `SPY` = `230`. (Match)
Since all stocks match, there are no discrepancies.


Expected Time Complexity:
The expected time complexity is O(L + K log K), where L is the total number of records across all lists, and K is the number of unique stock names.


Constraints:
0 <= N1, N2, N3 <= 1000
1 <= stock counts, values <= 10^9
Stock names are 1-10 uppercase letters.

Time limit: 1 sec
Approaches (1)
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Stock Ledger Reconciliation
Full screen
Console