0
0
HLDsystem_design~10 mins

Shard key selection in HLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select the shard key based on user ID.

HLD
shard_key = [1]
Drag options to blanks, or click blank then click option'
Asession_id
Btimestamp
Cuser_id
Drequest_type
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a non-unique or rapidly changing attribute like timestamp.
Using request_type which may not distribute data evenly.
2fill in blank
medium

Complete the code to ensure shard key supports range queries.

HLD
if shard_key == [1]:
    enable_range_queries = True
Drag options to blanks, or click blank then click option'
Arequest_type
Buser_id
Csession_id
Dtimestamp
Attempts:
3 left
💡 Hint
Common Mistakes
Using user_id which is not ordered.
Choosing session_id which may not be sequential.
3fill in blank
hard

Fix the error in shard key selection to avoid hotspotting.

HLD
shard_key = [1]  # Avoid using this key to prevent hotspots
Drag options to blanks, or click blank then click option'
Afixed_value
Btimestamp
Cregion_code
Duser_id
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing fixed_value which leads to all data in one shard.
Using region_code which might cause uneven distribution if skewed.
4fill in blank
hard

Fill both blanks to create a shard key that balances distribution and supports queries.

HLD
shard_key = ([1], [2])  # Composite shard key
Drag options to blanks, or click blank then click option'
Auser_id
Btimestamp
Cregion_code
Dsession_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using two non-unique or non-distributed keys.
Choosing keys that do not support query patterns.
5fill in blank
hard

Fill all three blanks to define shard key selection logic with validation.

HLD
def select_shard_key(record):
    key = record.get([1])
    if key is None or not isinstance(key, [2]):
        raise ValueError('Invalid shard key')
    return key if key [3] 0 else None
Drag options to blanks, or click blank then click option'
A'user_id'
Bint
C>
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key name or type.
Missing validation for key presence or type.
Using incorrect comparison operator.