Design
Design problems test your ability to implement custom data structures and systems that meet specific requirements. These problems focus on API design, data structure choice, and algorithmic optimization.Core Concepts
Key Skills
- Requirements Analysis: Understand constraints and operations
- Data Structure Selection: Choose right structures for efficiency
- API Design: Clean, intuitive interfaces
- Trade-offs: Balance time, space, and complexity
Common Design Patterns
- Cache Systems: LRU, LFU caches
- Rate Limiters: Token bucket, sliding window
- Data Stream: Moving average, median finder
- Hash + LinkedList: Combined for O(1) operations
Key Patterns & Techniques
1. Hash Map + Doubly Linked List
- Used in LRU cache
- O(1) access, insertion, deletion
- Hash map for O(1) lookup
- DLL for O(1) reordering
2. Multiple Data Structures
- Combine structures for different operations
- Trade space for time efficiency
- Each structure optimizes specific operation
3. Lazy Evaluation
- Defer computation until needed
- Amortized time complexity
- Cache computed results
4. Circular Buffer
- Fixed-size sliding window
- Efficient for streaming data
- Overwrite old data automatically
Common Approaches
LRU Cache
Moving Average from Data Stream
Design HashMap
Min Stack
Problems in This Category
039. LRU Cache
Medium | Frequency: 62.4%Hash map + doubly linked list for O(1) get and put operations.
048. Moving Average from Data Stream
Easy | Frequency: 56.0%Queue and running sum for O(1) moving average calculation.
Design Interview Tips
Approach Methodology
-
Clarify Requirements
- What operations are needed?
- What are the time/space constraints?
- What’s the expected usage pattern?
- Edge cases and scale?
-
Choose Data Structures
- List operations and required time complexity
- Select structures that optimize critical operations
- Consider trade-offs
-
Design API
- Clear, intuitive method names
- Consistent return types
- Handle edge cases
-
Implement and Test
- Start with core functionality
- Add edge case handling
- Test with examples
Common Patterns
| Problem Type | Data Structures | Key Insight |
|---|---|---|
| LRU Cache | Hash + DLL | O(1) access + reorder |
| LFU Cache | Hash + Min Heap | Track frequency |
| Moving Average | Queue + Sum | Circular buffer |
| Min/Max Stack | Stack + Auxiliary | Store min/max at each level |
| Design HashMap | Array + Chaining | Handle collisions |
| Time-based KV Store | Hash + TreeMap | Binary search on time |
Trade-off Considerations
Time vs Space
Simplicity vs Optimization
Advanced Design Patterns
LFU Cache
Rate Limiter (Token Bucket)
Time-based Key-Value Store
Common Interview Questions
Initial Questions to Ask
- What operations are needed and their frequency?
- What’s the expected scale (number of items/operations)?
- What are the time complexity requirements?
- What happens on edge cases (empty, full, duplicates)?
- Are there any special constraints (memory, thread-safety)?
Design Checklist
- Clarified all requirements
- Identified critical operations
- Chose appropriate data structures
- Defined clear API
- Handled edge cases
- Analyzed time/space complexity
- Considered trade-offs
- Tested with examples
Pro Tip: For design problems, start by listing all required operations with target time complexity. Then work backwards to choose data structures that enable those complexities. Often the solution combines multiple structures, each optimizing different operations.