0
0
HLDsystem_design~10 mins

Database sharding strategies 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 identify the sharding key in a database shard.

HLD
shard_key = request.get('[1]')
Drag options to blanks, or click blank then click option'
Asession_id
Btimestamp
Ctransaction_id
Duser_id
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a timestamp as shard key can cause hotspotting.
Using session_id may not distribute data evenly.
2fill in blank
medium

Complete the code to calculate the shard number using modulo sharding.

HLD
shard_number = hash(user_id) [1] total_shards
Drag options to blanks, or click blank then click option'
A+
B%
C/
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using division instead of modulo causes float results.
Adding hash and total_shards does not map to a shard.
3fill in blank
hard

Fix the error in the shard lookup function to correctly route requests.

HLD
def get_shard(user_id):
    shard_id = hash(user_id) [1] num_shards
    return shards[shard_id]
Drag options to blanks, or click blank then click option'
A/
B+
C%
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using division returns float, causing index errors.
Multiplying or adding does not limit shard_id range.
4fill in blank
hard

Fill both blanks to implement range-based sharding for user IDs.

HLD
if user_id [1] 1000:
    shard = shards[[2]]
Drag options to blanks, or click blank then click option'
A<
B>
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < reverses the condition.
Assigning shard 1 instead of 0 for this range.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension for consistent hashing buckets.

HLD
buckets = [1]: hash([2]) [3] num_buckets for [2] in nodes}
Drag options to blanks, or click blank then click option'
Anode
C%
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of modulo for bucket assignment.
Mismatching key and iteration variable names.