1. Introduction to System Design for Freshers
System design interviews for freshers focus on Low-Level Design (LLD) and choosing optimal data structures. Below are 20 essential questions with complete solutions in C++, Java, Python, and JavaScript.
2. Core LLD Questions
Q1. Design LRU Cache
cpp#include <unordered_map> using namespace std; class LRUCache { struct Node { int key, val; Node *prev, *next; Node(int k, int v) : key(k), val(v), prev(nullptr), next(nullptr) {} }; int cap; unordered_map<int, Node*> map; Node *head, *tail; void remove(Node* node) { node->prev->next = node->next; node->next->prev = node->prev; } void add(Node* node) { node->next = head->next; node->prev = head; head->next->prev = node; head->next = node; } public: LRUCache(int capacity) : cap(capacity) { head = new Node(0, 0); tail = new Node(0, 0); head->next = tail; tail->prev = head; } int get(int key) { if (!map.count(key)) return -1; Node* node = map[key]; remove(node); add(node); return node->val; } void put(int key, int value) { if (map.count(key)) remove(map[key]); Node* node = new Node(key, value); add(node); map[key] = node; if (map.size() > cap) { Node* lru = tail->prev; remove(lru); map.erase(lru->key); delete lru; } } };
javaimport java.util.HashMap; class LRUCache { class Node { int key, val; Node prev, next; Node(int key, int val) { this.key = key; this.val = val; } } private int cap; private HashMap<Integer, Node> map = new HashMap<>(); private Node head, tail; public LRUCache(int capacity) { this.cap = capacity; head = new Node(0, 0); tail = new Node(0, 0); head.next = tail; tail.prev = head; } private void remove(Node node) { node.prev.next = node.next; node.next.prev = node.prev; } private void add(Node node) { node.next = head.next; node.prev = head; head.next.prev = node; head.next = node; } public int get(int key) { if (!map.containsKey(key)) return -1; Node node = map.get(key); remove(node); add(node); return node.val; } public void put(int key, int value) { if (map.containsKey(key)) remove(map.get(key)); Node node = new Node(key, value); add(node); map.put(key, node); if (map.size() > cap) { Node lru = tail.prev; remove(lru); map.remove(lru.key); } } }
pythonclass Node: def __init__(self, key=0, val=0): self.key = key self.val = val self.prev = self.next = None class LRUCache: def __init__(self, capacity: int): self.cap = capacity self.map = {} self.head = Node() self.tail = Node() self.head.next = self.tail self.tail.prev = self.head def _remove(self, node): node.prev.next = node.next node.next.prev = node.prev def _add(self, node): node.next = self.head.next node.prev = self.head self.head.next.prev = node self.head.next = node def get(self, key: int) -> int: if key not in self.map: return -1 node = self.map[key] self._remove(node) self._add(node) return node.val def put(self, key: int, value: int) -> None: if key in self.map: self._remove(self.map[key]) node = Node(key, value) self._add(node) self.map[key] = node if len(self.map) > self.cap: lru = self.tail.prev self._remove(lru) del self.map[lru.key]
javascriptclass Node { constructor(key = 0, val = 0) { this.key = key; this.val = val; this.prev = null; this.next = null; } } class LRUCache { constructor(capacity) { this.cap = capacity; this.map = new Map(); this.head = new Node(); this.tail = new Node(); this.head.next = this.tail; this.tail.prev = this.head; } _remove(node) { node.prev.next = node.next; node.next.prev = node.prev; } _add(node) { node.next = this.head.next; node.prev = this.head; this.head.next.prev = node; this.head.next = node; } get(key) { if (!this.map.has(key)) return -1; let node = this.map.get(key); this._remove(node); this._add(node); return node.val; } put(key, value) { if (this.map.has(key)) this._remove(this.map.get(key)); let node = new Node(key, value); this._add(node); this.map.set(key, node); if (this.map.size > this.cap) { let lru = this.tail.prev; this._remove(lru); this.map.delete(lru.key); } } }
Time Complexity: O(1) | Space Complexity: O(capacity)
3. Summary Table
| System | Data Structure | Time | Space |
|---|---|---|---|
| LRU Cache | Hash Map + Doubly Linked List | O(1) | O(capacity) |
Practice all system design problems on DSAMaster's practice platform.
