FastPrepTop K Keys from JSON Records
Problems/Top K Keys from JSON Records

Top K Keys from JSON Records

Capital One logoCapital One● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The coding question in a 2025 Capital One intern Power Day report is a top-K task on JSON input, with hash maps as the expected tool.

The report does not show the record fields, what to rank by, or how to break ties. Here, each record has a key and a value. Keys are ranked by the sum of their values, and a larger total comes first. Equal totals put the smaller key first. The input has up to 2000 records, and spaces and field order can vary.

Our version is about 70% the same as the reported question.

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

  • records holds between 1 and 2000 records.
  • Each key is 1 to 10 lowercase English letters.
  • Each value is an integer from 0 to 10^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)
All Capital One interview questionsSee Capital One hiring insights
Saved
class Solution {
    public String[] topKKeys(String records, int k) {
        // Write your code here.
    }
}

Not a valid String

Checking account…