Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Design an In-Memory Key-Value Store | Machine Coding Round Questions (SDE II/III)

Gaurav Chandak
Gaurav Chandak

Machine Coding Round Complete Guide

  • What is a Machine Coding Round?
  • How to prepare for Machine Coding Round?
  • How to clear the Machine Coding Round?
  • Machine Coding Round Interview Practice Questions
  • Machine Coding Round Interview Solutions/Editorials

Problem Statement

Design an In-Memory Key-Value Store like Redis.

Requirements

  • The key-value store will be in-memory and does not require access to the file system.
  • The key will always be a string.
  • The value would be an object/map. The object would have attributes and corresponding values.
    Example => "sde_bootcamp": { "title": "SDE-Bootcamp", "price": 30000.00, "enrolled": false, "estimated_time": 30 }
  • Each attribute key would be a string and the attribute values could be string, integer, double or boolean.
  • The key-value store should be thread-safe.
  • The Key-Value store should expose the following functions:
    • get(String key) => Should return the value (object with attributes and their values). Return null if key not present
    • search(String attributeKey, String attributeValue) => Returns a list of keys that have the given attribute key, value pair.
    • put(String key, List<Pair<String, String>> listOfAttributePairs) => Adds the key and the attributes to the key-value store. If the key already exists then the value is replaced.
    • delete(String key) => Deletes the key, value pair from the store.
    • keys() => Return a list of all the keys
  • The value object should override the toString method to print the object as a comma-separated list of key-value pairs for the attributes.
    Example: attribute1: attribute_value_1, attribute2: attribute_value_2, attribute3: attribute_value_3
  • The data type of an attribute should get fixed after its first occurrence. Example: Once we encounter an attribute age with an integer value then any entry with an age attribute having a non-integer value should result in an exception.
  • Nothing should be printed inside any of these methods. All scanning and printing should happen in the Driver/Main class only. Exception Handling should also happen in the Driver/Main class.

Input/Output Format

The code should strictly follow the input/output format and will be tested with provided test cases.

Input Format

Multiple lines with each line containing a command.

Possible commands:

  • get <key>
  • put <key> <attributeKey1> <attributeValue1> <attributeKey2> <attributeValue2>....
  • delete <key>
  • search <attributeKey> <attributeValue>
  • keys
  • exit

Stop taking the input when you encounter the word exit.

Assume that attribute keys and values would not have space in between.

Output Format

Print output based on the specific commands as mentioned below.

get

Comma and space-separated attributes. Example:

attribute1: attribute_value_1, attribute2: attribute_value_2, attribute3: attribute_value_3

Print "No entry found for <key>" if get returns null.

put

Do not print anything. Print "Data Type Error" if attribute has data type other than previous set.

delete

Do not print anything.

search

Comma-separated keys. Example:

key1,key2,key3,key4

Print in sorted order

keys

Comma-separated keys. Example:

key1,key2,key3,key4

Print in sorted order

Example

Sample Input

put sde_bootcamp title SDE-Bootcamp price 30000.00 enrolled false estimated_time 30
get sde_bootcamp
keys
put sde_kickstart title SDE-Kickstart price 4000 enrolled true estimated_time 8
get sde_kickstart
keys
put sde_kickstart title SDE-Kickstart price 4000.00 enrolled true estimated_time 8
get sde_kickstart
keys
delete sde_bootcamp
get sde_bootcamp
keys
put sde_bootcamp title SDE-Bootcamp price 30000.00 enrolled true estimated_time 30
search price 30000.00
search enrolled true

Expected Output

title: SDE-Bootcamp, price: 30000.00, enrolled: false, estimated_time: 30
sde_bootcamp
Data Type Error
No entry found for sde_kickstart
sde_bootcamp
title: SDE-Kickstart, price: 4000.00, enrolled: true, estimated_time: 8
sde_bootcamp,sde_kickstart
No entry found for sde_bootcamp
sde_kickstart
sde_bootcamp
sde_bootcamp,sde_kickstart

Expectations

  • Make sure that you have a working and demonstrable code
  • Make sure that the code is functionally correct
  • Code should be modular and readable
  • Separation of concern should be addressed
  • Please do not write everything in a single file (if not coding in C/C++)
  • Code should easily accommodate new requirements and minimal changes
  • There should be a main method from where the code could be easily testable
  • [Optional] Write unit tests, if possible
  • No need to create a GUI
1
Gaurav Chandak
Gaurav Chandak
Gaurav is the co-founder of workat.tech and has previously worked at Flipkart and Microsoft. He intends to actively contribute to the future of education through workat.tech.
Related Content
Design a Distributed Queue | Kafka | Machine Coding Round Questions (SDE II/III)
Design Trello | Machine Coding Round Questions (SDE II)
Design Splitwise | Machine Coding Round Questions (SDE I/II)
SDE Bootcamp - Become a software engineer at a product-based company
Practice Machine Coding
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet