Design a Scalable Queue with Fixed-Size Block Constraint

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2019-06-21

Problem Statement

Design and implement a Queue data structure that scales efficiently while adhering to a fixed-size block (or segment) constraint. Instead of storing all elements in a single contiguous array or a simple linked list, the queue will internally use multiple fixed-size blocks (arrays of predefined capacity) to store elements. This design improves scalability, avoids costly resizing operations, and ensures predictable memory usage.


✅ Functional Requirements

1.<span></span>Queue Operations
2.<span></span>enqueue(element) → Add an element to the end of the queue.
3.<span></span>dequeue() → Remove and return the front element.
4.<span></span>peek() → Return the front element without removing it.
5.<span></span>isEmpty() → Return whether the queue is empty.
6.<span></span>size() → Return the number of elements currently in the queue.
7.<span></span>Fixed-Size Block Behavior
8.<span></span>The queue is internally backed by blocks of fixed capacity (e.g., 1000 elements per block).
9.<span></span>When a block is filled, a new block is allocated and linked to the queue.
10.<span></span>When a block becomes empty after dequeues, it can be released for reuse or garbage collection.
11.<span></span>Scalability
12.<span></span>The queue should support millions of elements efficiently.
13.<span></span>It should grow/shrink seamlessly by allocating and deallocating blocks.
14.<span></span>Configurable Block Size
15.<span></span>Block size is set at initialization.
16.<span></span>Should work for small (e.g., 10) and large (e.g., 10,000) block sizes.


⚙️ Non-Functional Requirements

1.<span></span>Performance:
2.<span></span>Enqueue/Dequeue in amortized O(1).
3.<span></span>No expensive full-array copy operations.
4.<span></span>Memory Efficient: Allocate blocks only when required.
5.<span></span>Extensibility: Design should allow future extensions (e.g., thread safety, priority queue).


🔐 Constraints

1.<span></span>Generic data type support.
2.<span></span>Block size ≥ 1.
3.<span></span>Must handle large-scale data (10⁶+ elements).


🧱 Key Classes / Components to Model

1.<span></span>Block
2.<span></span>Fixed-size array.
3.<span></span>Start index, end index pointers.
4.<span></span>Pointer to next block (and optionally previous).
5.<span></span>ScalableQueue
6.<span></span>Maintains head and tail block references.
7.<span></span>Tracks current size.
8.<span></span>Methods:
9.<span></span>enqueue(T element)
10.<span></span>T dequeue()
11.<span></span>T peek()
12.<span></span>boolean isEmpty()
13.<span></span>int size()


🧪 Example Walkthrough

<div><div><br /></div><div>Block Size = 3</div><div><br /></div><div>Enqueue(10) → [10]</div><div>Enqueue(20) → [10,20]</div><div>Enqueue(30) → [10,20,30]</div><div>Enqueue(40) → New Block Allocated</div><div> Block1 = [10,20,30]</div><div> Block2 = [40]</div><div><br /></div><div>Dequeue() → returns 10</div><div> Block1 = [20,30]</div><div> Block2 = [40]</div></div>


👉 This LLD problem tests a candidate’s ability to:

1.<span></span>Design scalable, memory-efficient data structures.
2.<span></span>Use modular OOP design (separating Block from Queue).
3.<span></span>Handle dynamic growth and shrinkage with minimal overhead.


Solution

Comments