Bird
Raised Fist0
HLDsystem_design~20 mins

Design a unique ID generator in HLD - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Unique ID Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Architecture
intermediate
2:00remaining
Identify the correct architecture for a distributed unique ID generator

You want to design a system that generates unique IDs across multiple servers without collisions. Which architecture below best ensures uniqueness and scalability?

AA centralized server generates IDs sequentially and all clients request IDs from it.
BClients generate random IDs independently without coordination.
CEach server generates IDs using a timestamp combined with a unique server ID and a sequence number.
DIDs are generated by hashing client IP addresses and timestamps.
Attempts:
2 left
💡 Hint

Think about how to avoid collisions and maintain scalability without a single bottleneck.

scaling
intermediate
2:00remaining
Estimate the capacity of a unique ID generator system

Your unique ID generator uses 41 bits for timestamp (in milliseconds), 10 bits for machine ID, and 12 bits for sequence number per millisecond. How many unique IDs can the system generate per millisecond?

A4096 IDs per second
B4096 IDs per millisecond
C1024 IDs per second
D1024 IDs per millisecond
Attempts:
2 left
💡 Hint

Calculate how many IDs the sequence number allows per millisecond, then convert to per second.

tradeoff
advanced
2:00remaining
Choose the best tradeoff for ID length vs. uniqueness duration

You design a unique ID with 64 bits total. You can allocate bits between timestamp and sequence number. Allocating more bits to timestamp increases the time range IDs remain unique, but reduces IDs per millisecond. Allocating more bits to sequence number increases IDs per millisecond but shortens uniqueness duration. Which allocation is best for a system needing 10,000 IDs per millisecond and uniqueness for 10 years?

A39 bits timestamp, 14 bits sequence number
B41 bits timestamp, 12 bits sequence number
C42 bits timestamp, 11 bits sequence number
D38 bits timestamp, 15 bits sequence number
Attempts:
2 left
💡 Hint

Calculate if the timestamp bits cover 10 years and if sequence bits cover 10,000 IDs per millisecond.

🧠 Conceptual
advanced
2:00remaining
Identify the main challenge in distributed unique ID generation

What is the biggest challenge when designing a distributed unique ID generator that works across many servers?

AEnsuring IDs are cryptographically secure
BMinimizing the size of the ID to 16 bits
CGenerating IDs that are human-readable
DAvoiding collisions without a central coordinator
Attempts:
2 left
💡 Hint

Think about what happens when multiple servers generate IDs independently.

component
expert
3:00remaining
Trace the request flow in a Snowflake-like ID generator system

In a Snowflake-like distributed ID generator, which sequence correctly describes the request flow when a client requests a new ID?

A1,2,3,4,5,6
B1,3,2,4,5,6
C2,1,3,4,5,6
D1,2,4,3,5,6
Attempts:
2 left
💡 Hint

Think about the logical order from request to ID generation and response.

Practice

(1/5)
1. What is the primary purpose of a unique ID generator in a distributed system?
easy
A. To create identifiers that are distinct across all machines and time
B. To encrypt data for secure communication
C. To compress large files efficiently
D. To balance load between servers

Solution

  1. Step 1: Understand the role of unique IDs

    Unique IDs ensure that each identifier is different from others, avoiding conflicts.
  2. Step 2: Recognize distributed system needs

    In distributed systems, IDs must be unique across machines and time to prevent collisions.
  3. Final Answer:

    To create identifiers that are distinct across all machines and time -> Option A
  4. Quick Check:

    Unique ID purpose = distinct identifiers [OK]
Hint: Unique IDs prevent duplicates across systems [OK]
Common Mistakes:
  • Confusing unique ID with encryption
  • Thinking unique ID compresses data
  • Mixing load balancing with ID generation
2. Which of the following is a common component in a unique ID generator design?
easy
A. Encryption key for data security
B. Load balancer to distribute requests
C. Compression algorithm for data size reduction
D. Sequence number to avoid collisions within the same timestamp

Solution

  1. Step 1: Identify components of unique ID generators

    Common components include timestamp, machine identifier, and sequence number.
  2. Step 2: Understand sequence number role

    Sequence numbers help generate multiple unique IDs within the same timestamp to avoid collisions.
  3. Final Answer:

    Sequence number to avoid collisions within the same timestamp -> Option D
  4. Quick Check:

    Sequence number = collision avoidance [OK]
Hint: Sequence numbers prevent same-time ID clashes [OK]
Common Mistakes:
  • Confusing encryption with ID generation
  • Thinking compression is part of ID design
  • Mixing load balancing with ID components
3. Consider a unique ID generator that uses a 41-bit timestamp, 10-bit machine ID, and 12-bit sequence number. What is the maximum number of unique IDs it can generate per millisecond per machine?
medium
A. 8192
B. 1024
C. 4096
D. 2048

Solution

  1. Step 1: Understand bit allocation for sequence number

    The sequence number uses 12 bits, so max IDs per millisecond = 2^12.
  2. Step 2: Calculate 2^12

    2^12 = 4096 unique IDs per millisecond per machine.
  3. Final Answer:

    4096 -> Option C
  4. Quick Check:

    2^12 = 4096 [OK]
Hint: 2^sequence_bits = max IDs/ms [OK]
Common Mistakes:
  • Using machine ID bits instead of sequence bits
  • Calculating 2^10 or 2^11 instead of 2^12
  • Confusing total bits with sequence bits
4. A unique ID generator uses a timestamp, machine ID, and sequence number. If two machines generate IDs at the exact same millisecond with the same sequence number, what is the likely cause of duplicate IDs?
medium
A. Machine IDs are not unique or not included in the ID
B. Timestamp is too large
C. Sequence number is too long
D. The system uses encryption

Solution

  1. Step 1: Analyze ID components for uniqueness

    Machine ID differentiates IDs from different machines at the same time.
  2. Step 2: Identify cause of duplicates

    If machine IDs are missing or not unique, IDs from different machines can collide.
  3. Final Answer:

    Machine IDs are not unique or not included in the ID -> Option A
  4. Quick Check:

    Missing unique machine ID = duplicates [OK]
Hint: Unique machine ID prevents cross-machine duplicates [OK]
Common Mistakes:
  • Blaming timestamp size for duplicates
  • Thinking longer sequence number causes duplicates
  • Confusing encryption with ID uniqueness
5. You need to design a unique ID generator for a global system with thousands of machines generating millions of IDs per second. Which design choice best ensures scalability and uniqueness?
hard
A. Generate random 64-bit numbers without coordination
B. Use a 64-bit ID combining timestamp, machine ID, and sequence number with synchronized clocks
C. Use only timestamp-based IDs without machine info
D. Assign IDs sequentially from a central server

Solution

  1. Step 1: Consider scalability and uniqueness needs

    Global scale requires IDs unique across machines and time, with high throughput.
  2. Step 2: Evaluate design options

    Combining timestamp, machine ID, and sequence number in 64 bits with synchronized clocks ensures uniqueness and scalability.
  3. Step 3: Reject other options

    Random IDs risk collisions; timestamp-only lacks machine uniqueness; central server causes bottleneck.
  4. Final Answer:

    Use a 64-bit ID combining timestamp, machine ID, and sequence number with synchronized clocks -> Option B
  5. Quick Check:

    64-bit composite ID = scalable unique IDs [OK]
Hint: Combine time, machine, sequence for scalable unique IDs [OK]
Common Mistakes:
  • Relying on random IDs risking collisions
  • Ignoring machine ID causing duplicates
  • Using central server causing bottlenecks