Serialize and Deserialize a String Key-Value Store
Updated
Problem statement
Write both sides of an encoder and decoder for a key-value store where every key and value is a string. Your function gets a list of operations. Handle each operation on its own and return one row of strings for it. Nothing carries over from one operation to the next.
serialize: its payload row is a flat list[key0, value0, key1, value1, ...], and no key repeats. Return a row with exactly one string: the encoded store.deserialize: its payload row holds exactly one string. If that string is valid, return the decoded flat key/value list, with keys sorted in ascending ASCII order. If it is not valid, return exactly["ERROR"].
Keys and values are printable ASCII strings. They may be empty, and they may contain the delimiter characters used below. When you serialize, write the keys in ascending ASCII order. Upper- and lowercase letters are different characters.
Wire format
An encoded store is N; followed by exactly N records. Each record is keyLength:keyvalueLength:value. Read the key length, then that many characters for the key. Then read the value length, then that many characters for the value. Records follow each other with no separator. Lengths count characters, not how JSON would escape them.
The count is between 0 and 20, and each key or value length is between 0 and 40. A number is 0, or a nonzero digit followed by zero or more digits. Signs, spaces and leading zeros are not allowed. A decoder accepts the records in any order. It rejects duplicate keys, missing delimiters, cut-off fields, numbers out of range, and any extra characters at the end. Only a complete, valid store gives a decoded result.
The empty store encodes to 0; and decodes to an empty row. A valid decoded row always has an even number of items, so it can never be confused with the one-item error row. Do not just split on the delimiters: they can also appear inside keys and values.
Function
kvCodec(operations: String[], payloads: String[][]) → String[][]Examples
Example 1
operations = ["serialize","deserialize","serialize","deserialize"]payloads = [["b","two","a","one"],["2;1:a3:one1:b3:two"],[],["0;"]]return = [["2;1:a3:one1:b3:two"],["a","one","b","two"],["0;"],[]]The keys are sorted first, so a comes before b. Both keys have length 1 and both values have length 3, which gives 2;1:a3:one1:b3:two. The last two operations show the empty store in both directions.
Example 2
operations = ["serialize","deserialize"]payloads = [["k:;","v;:","",""],["2;3:k:;3:v;:0:0:"]]return = [["2;0:0:3:k:;3:v;:"],["","","k:;","v;:"]]The empty key sorts before k:;. The other key and its value each have length 3, counting their delimiter characters. The given wire lists the non-empty key first, but decoding returns the keys sorted and keeps every character.
Example 3
operations = ["deserialize","deserialize","deserialize","deserialize"]payloads = [["1;1:a2:x"],["2;1:a1:x1:a1:y"],["01;1:a1:x"],["0;x"]]return = [["ERROR"],["ERROR"],["ERROR"],["ERROR"]]The four wires have, in order: a value that is cut short, a duplicate key, a count with a leading zero, and an extra character after an empty store. Each one is rejected as a whole, with no partial result.
Constraints
0 <= operations.length <= 40.payloads.length == operations.length.0 <= payloads[i].length <= 40.- Every payload string has at most
2500characters. - Every operation is exactly
serializeordeserialize. - Every input string contains only ASCII characters with codes from
32through126. - For a serialize operation, its row has even length, contains at most 20 distinct key/value pairs, and each key or value has at most 40 characters.
- For a deserialize operation, its row contains exactly one wire string of at most
2500characters. That wire may be malformed.
More ByteDance Onsite Interview questions
- Search a Row- and Column-Sorted MatrixONSITE INTERVIEW · Seen Nov 2026
- Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Implement Multi-Head AttentionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dynamic Median with Additions and DeletionsONSITE INTERVIEW · Seen Oct 2026
- Binary Tree Maximum Path SumONSITE INTERVIEW · Seen Oct 2026
- Merge Overlapping IntervalsONSITE INTERVIEW · Seen Oct 2026
- Tic-Tac-Toe Move WinnersONSITE INTERVIEW · Seen Oct 2026
- Kth Largest Element without a HeapONSITE INTERVIEW · Seen Oct 2026
- Least Recently Used CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Number of AtomsAmazon · ONSITE INTERVIEW
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Common Words Across FilesMeta · PHONE SCREENMeta interview questions
- Brazilian Receivables Part 3 — Partial ContractsStripe · PHONE SCREENStripe interview questions
- Summarize Server Healthcheck LogsNVIDIA · PHONE SCREENNVIDIA interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Validate Server Log Summary CountsGoogle · ONSITE INTERVIEWGoogle interview questions
ByteDance interview experiences
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience
- ByteDance ML Intern Phone Screen: 1 Experience