Stockroom reconcile
MediumOnline Assessment
Reconcile a store's stock ledger with the physical count taken overnight and report only the discrepancies that matter.
Both inputs are lists of strings "sku quantity": a SKU code (letters and digits) and a whole number of units. SKU codes are case-insensitive (ab12 and AB12 are the same item) and are reported in uppercase. A SKU may appear on several lines of the same list, for example a count made on several shelves; add those quantities up.
For a SKU that is in ledger, the allowed tolerance is the larger of tol_units and ledger_total * tol_percent // 100 (integer division). A SKU missing from the count has a counted total of 0. The variance is counted - ledger; it is acceptable if its absolute value is at most the tolerance, and then nothing is reported.
Otherwise report:
"SKU SHORT n"if fewer units were counted than the ledger says,nbeing the shortfall;"SKU OVER n"if more were counted,nbeing the surplus.
A SKU that appears in the count but nowhere in the ledger is reported as "SKU UNKNOWN n" with n its counted total, whatever the tolerance, unless that total is 0. A SKU that is in the ledger with a total of 0 is a known SKU.
Input: ledger and counted, lists of strings in the format above, and the integers tol_units and tol_percent.
Output: the report lines, ordered by n descending and, for equal n, by SKU in ascending string order. If everything is within tolerance, return [].
Example 1
- Input:
- [["A1 200","B2 10","C3 5"],["A1 197","B2 7"],1,2]
- Output:
- ["C3 SHORT 5","B2 SHORT 3"]
- Explanation
- With 1 unit or 2% the tolerance for A1 (200 units) is max(1, 4) = 4: the count of 197 is 3 short, which is acceptable. B2 has 10 units, tolerance max(1, 0) = 1, and 7 were counted, so it is 3 SHORT. C3 is missing from the count, 5 short against tolerance 1. The two reported lines are 5 then 3 by size.
Example 2
- Input:
- [["z9 10","q7 5"],["z9 6","z9 4","Q7 12","n1 3"],3,0]
- Output:
- ["Q7 OVER 7","N1 UNKNOWN 3"]
- Explanation
- Shelf lines add up: z9 is 6 + 4 = 10 counted against 10 in the ledger, fine. The SKU is case-insensitive, so "q7" and "Q7" are one item with a ledger total of 5 and 12 counted, 7 OVER. n1 is not in the ledger at all and is UNKNOWN with 3, even though 3 is within the unit tolerance of 3.
Example 3
- Input:
- [["d4 0","f6 45"],["d4 2","e5 0","f6 50"],2,10]
- Output:
- ["F6 OVER 5"]
- Explanation
- d4 is in the ledger with 0 units, so it is a known SKU: 2 counted is within tol_units 2 and is not reported, while 3 counted would be 3 OVER. e5 is not in the ledger but its counted total is 0, so it is ignored. f6 has 45 in the ledger and 50 counted: the tolerance is max(2, 45 * 10 // 100 = 4) = 4, so the surplus of 5 is reported.
Constraints
0 <= ledger.length, counted.length <= 1,500- SKU codes are 1 to 8 letters and digits;
0 <= quantity <= 1,000,000 0 <= tol_units <= 100,0 <= tol_percent <= 100