Top K Keys from JSON Records
Updated
Problem statement
The input is records, a JSON text containing a list of records, together with a whole number k.
Each record is a JSON object with exactly two fields:
"key": a string of lowercase English letters."value": a non-negative integer.
The two fields can appear in either order inside a record.
The total of a key is the sum of the values in all records that carry it; a hash map from key to total is the natural place to keep these. Key x outranks key y when x has the larger total, or when their totals are equal and x comes first alphabetically. A total of 0 takes part in the ranking like any other.
Return the first k keys of this order, highest first.
Function
topKKeys(records: String, k: int) → String[]Examples
Example 1
records = "[{\"key\":\"coffee\",\"value\":4},{\"key\":\"books\",\"value\":10},{\"key\":\"coffee\",\"value\":7},{\"key\":\"games\",\"value\":3}]"k = 2return = ["coffee","books"]Summing per key gives coffee = 4 + 7 = 11, books = 10 and games = 3. With k = 2, coffee ranks first, books second, and games is left out.
Example 2
records = "[ {\"value\": 5, \"key\": \"rent\"}, {\"key\": \"food\", \"value\": 2}, {\"value\": 3, \"key\": \"food\"} ]"k = 2return = ["food","rent"]Some records list "value" before "key", and there are spaces between tokens. rent sums to 5 and food sums to 2 + 3 = 5. With the sums level, alphabetical order decides: food sorts before rent.
Example 3
records = "[{\"key\":\"b\",\"value\":0},{\"key\":\"a\",\"value\":0},{\"key\":\"c\",\"value\":1}]"k = 2return = ["c","a"]Every key takes part, even with a zero total: c has 1, while a and b have 0 each. Ordered by total and then alphabetically, the full ranking is c, a, b. Keeping its first k = 2 entries gives ["c", "a"].
Constraints
recordsholds between1and2000records.- Each key is
1to10lowercase English letters. - Each value is an integer from
0to10^4, written without a sign, a decimal point or leading zeros. - Spaces may appear between tokens; there are no other whitespace characters.
1 <= k <=the number of distinct keys.
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Stable Top K Frequent WordsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Time-Aware Search AutocompleteRoblox · PHONE SCREENRoblox interview questions
- Top N Frequent Words in a FileMeta · PHONE SCREENMeta interview questions
- Sort Characters by How Often They AppearAdobe · PHONE SCREENAdobe interview questions
- Top K Frequent WordsBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
Capital One interview experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences
- Capital One TDP Agentic Power Day: 25 Experiences (Part 1 of 2)