PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCareers

Quick Overview

This question evaluates skills in designing and implementing an in-memory key-value store with serialization, testing data structure design, API design (set/get/serialize/deserialize), serialization format selection, type fidelity for common Python types, and robust error handling.

  • Medium
  • OpenAI
  • Coding & Algorithms
  • Software Engineer

Implement in-memory KV store with serialization

Company: OpenAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Implement an in-memory key-value store in Python that supports setting and retrieving values and can serialize and deserialize the entire store. Define a clear API (e.g., set(key, value), get(key), serialize(), deserialize(blob)). Choose a serialization format (e.g., JSON or pickle), explain your choice, and ensure type fidelity for common Python types (ints, floats, strings, lists, dicts). Handle missing keys and malformed or incompatible serialized input gracefully. Provide basic tests to demonstrate that data round-trips correctly.

Quick Answer: This question evaluates skills in designing and implementing an in-memory key-value store with serialization, testing data structure design, API design (set/get/serialize/deserialize), serialization format selection, type fidelity for common Python types, and robust error handling.

Implement a function `solution(operations)` that simulates an in-memory key-value store with the API `set(key, value)`, `get(key)`, `serialize()`, and `deserialize(blob)`. Use JSON as the serialization format because it is human-readable, portable, and safer than `pickle`. To preserve type fidelity, only allow values built from `int`, finite `float`, `str`, `list`, and `dict`, where every dict key is a string. The `deserialize` operation must accept only a JSON object representing the entire store. Process the operations in order and return a list containing one result per operation: - `set(key, value)` -> `True` if the value is valid and stored, otherwise `False` and the store is unchanged. - `get(key)` -> the stored value, or `None` if the key does not exist. - `serialize()` -> a compact JSON string of the whole store with keys sorted lexicographically. - `deserialize(blob)` -> `True` if `blob` is valid and compatible, replacing the whole store; otherwise `False` and the store is unchanged.

Constraints

  • 0 <= len(operations) <= 10^4
  • Store keys and all nested dict keys must be strings
  • Supported stored values are int, finite float, str, list, and dict with recursive nesting
  • The total size of all values and serialized blobs processed is at most 2 * 10^5

Examples

Input: [('set', 'count', 5), ('set', 'info', {'name': 'Ada', 'scores': [1, 2.5]}), ('serialize',), ('deserialize', '{"count":5,"info":{"name":"Ada","scores":[1,2.5]}}'), ('get', 'info')]

Expected Output: [True, True, '{"count":5,"info":{"name":"Ada","scores":[1,2.5]}}', True, {'name': 'Ada', 'scores': [1, 2.5]}]

Explanation: Two values are stored, the whole store is serialized to deterministic JSON, deserialized back, and the nested object is retrieved unchanged.

Input: [('set', 'x', [1, 2, 3]), ('get', 'missing'), ('deserialize', '{"x":[1,2,3]'), ('get', 'x')]

Expected Output: [True, None, False, [1, 2, 3]]

Explanation: Getting a missing key returns None. The malformed JSON blob fails to deserialize, so the previous store remains intact.

Input: [('set', 'obj', {1: 'a'}), ('serialize',), ('get', 'obj')]

Expected Output: [False, '{}', None]

Explanation: The nested dict uses a non-string key, which would lose fidelity in JSON, so set fails and the store stays empty.

Input: [('set', 'a', 1), ('deserialize', '[1,2,3]'), ('get', 'a'), ('serialize',)]

Expected Output: [True, False, 1, '{"a":1}']

Explanation: The blob is valid JSON but incompatible because the store must deserialize from a JSON object, not a list.

Input: []

Expected Output: []

Explanation: No operations means no results.

Solution

def solution(operations):
    import json
    import math

    store = {}

    def is_supported(value):
        if isinstance(value, bool) or value is None:
            return False
        if isinstance(value, int):
            return True
        if isinstance(value, float):
            return math.isfinite(value)
        if isinstance(value, str):
            return True
        if isinstance(value, list):
            return all(is_supported(item) for item in value)
        if isinstance(value, dict):
            return all(isinstance(k, str) and is_supported(v) for k, v in value.items())
        return False

    results = []

    for op in operations:
        if not op:
            results.append(False)
            continue

        command = op[0]

        if command == 'set':
            if len(op) != 3:
                results.append(False)
                continue
            key, value = op[1], op[2]
            if not isinstance(key, str) or not is_supported(value):
                results.append(False)
            else:
                store[key] = value
                results.append(True)

        elif command == 'get':
            if len(op) != 2:
                results.append(None)
                continue
            results.append(store.get(op[1]))

        elif command == 'serialize':
            if len(op) != 1:
                results.append(False)
                continue
            results.append(json.dumps(store, sort_keys=True, separators=(',', ':'), allow_nan=False))

        elif command == 'deserialize':
            if len(op) != 2 or not isinstance(op[1], str):
                results.append(False)
                continue
            try:
                candidate = json.loads(op[1])
            except (TypeError, ValueError):
                results.append(False)
                continue

            if isinstance(candidate, dict) and is_supported(candidate):
                store = candidate
                results.append(True)
            else:
                results.append(False)

        else:
            results.append(False)

    return results

Time complexity: O(S), where S is the total size of all values and JSON blobs processed across all operations. Space complexity: O(S) for the store, parsed JSON, and recursion over nested values.

Hints

  1. Use a normal dictionary for the live store, and write one recursive helper that validates whether a value can be safely serialized without losing type information.
  2. Make serialization deterministic by sorting keys and removing extra whitespace, so exact string comparisons in tests are stable.
Last updated: Apr 30, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • Careers
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Simulate Infection Spread on a Grid - OpenAI (hard)
  • Implement Social Follow Recommendations - OpenAI (medium)
  • Build a Compose Rating Card - OpenAI (medium)
  • Convert IPv4 Ranges to CIDR Blocks - OpenAI (medium)
  • Implement Persistent KV Store Serialization - OpenAI (hard)