Coding with design constraints

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-04-30

Problem Statement

Design and implement a class or system that solves a specific problem under one or more of the following design constraints:


•Limited memory usage
•Immutability (no modification after creation)
•Thread-safety (works correctly under concurrent access)
•Restricted API usage (e.g., no built-in sorting, no additional data structures)
•Real-time response requirements (bounded latency)


You must clearly describe your design decisions and how your implementation adheres to the given constraints.


✅ Functional Requirements


Depending on the selected example scenario, the system should:


1.Perform the required operations (e.g., data storage, computation, transformation).
2.Adhere to one or more constraints explicitly stated.
3.Demonstrate correctness, efficiency, and design clarity.


⚙️ Non-Functional Requirements

Aspect with Description


Efficiency

Optimize time and/or space complexity within given limits.


Simplicity

Avoid unnecessary abstractions or violations of constraints.


Thread-Safety

Ensure correct behavior in concurrent scenarios (if required).


Extensibility

Design should allow extension if constraints change in future.


💡 Example Scenarios -----------------------------------------------------------


1. Design a Thread-Safe Counter


Constraint: Must be thread-safe without using built-in synchronization primitives like synchronized or Lock.

Goal: Increment and get counter value correctly under concurrent access.


2. Design an Immutable Stack


Constraint: The stack must be completely immutable (no internal state changes).

Goal: Implement push(), pop(), and peek() without modifying existing instances.


3. Design a Fixed-Size Cache (Memory Constraint)


Constraint: Cache can store a maximum of N items, evicting the least recently used item when full.

Goal: Maintain O(1) lookup and insertion operations (similar to an LRU cache).


4. Design a File Reader for Large Files


Constraint: The system cannot load the entire file into memory (stream-based design).

Goal: Implement methods to read large files line by line efficiently.


5. Design a Real-Time Data Processor


Constraint: Each operation must complete within a fixed time bound (e.g., <10 ms).

Goal: Process incoming events and output results continuously without delay accumulation.


🧠 Discussion Points (for Interview)

•What constraints are you optimizing for (time, space, concurrency, immutability)?
•What trade-offs did you make to satisfy them?
•How would the design change if a constraint was relaxed or strengthened?
•How do you ensure correctness under restricted API usage or memory?

🧾 Final Interview Prompt

Design and implement a system or class under strict design constraints.
For example, design a thread-safe counter without synchronization primitives, or an immutable data structure that supports updates without mutation.
Clearly explain the constraint, justify design choices, and demonstrate how your code satisfies both functional and non-functional requirements.


Solution

Comments