Track New Values in Sorted Order
Updated
Problem statement
Design a structure with one operation, add(x), which stores the integer x. Each call returns two things: whether x is "new" (never added before) or "old" (added by an earlier call), and all distinct values stored so far, in ascending order, after this call.
You are given values, where values[i] is the argument of the i-th call to add, starting from an empty structure. Return one row per call: the row starts with "new" or "old", followed by the stored distinct values in ascending numeric order, each written as a decimal string.
Function
trackAdds(values: int[]) → String[][]Examples
Example 1
values = [2,1,2,3,1]return = [["new","2"],["new","1","2"],["old","1","2"],["new","1","2","3"],["old","1","2","3"]]add(2): 2 is new, the values are[2].add(1): new,[1, 2].add(2): 2 was already added, so it is old; the values stay[1, 2].add(3): new,[1, 2, 3].add(1): old,[1, 2, 3].
Example 2
values = [7]return = [["new","7"]]- The only call adds a new value, so the result is one row:
["new", "7"].
Example 3
values = [5,5,5]return = [["new","5"],["old","5"],["old","5"]]- The first
add(5)is new; the next two are old. The values stay[5].
Constraints
1 <= values.length <= 500 <= values[i] <= 999
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Employee Ratings Management SystemAmazon · PHONE SCREEN
- Running Medians and PercentilesStackAdapt · PHONE SCREENStackAdapt interview questions
- Versioned ArrayTrueFoundry · PHONE SCREEN
- My Calendar IUber · ONSITE INTERVIEWUber interview questions
- Future-Effective Employee SalaryRippling · ONSITE INTERVIEWRippling interview questions
- Chronological Sliding-Window Request CounterCareem · ONSITE INTERVIEWCareem interview questions
- Run-Length Integer Map: Get and SetAdobe · OAAdobe interview questions
- Array-Backed Set Without Library CollectionsOde with Anthropic · PHONE SCREEN
- Price at Query TimestampsGoogle · PHONE SCREENGoogle interview questions
- First Missing Number in a Consecutive ArrayArista Networks · OA · PHONE SCREENArista Networks interview questions