Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a timestamp as shard key can cause hotspotting.
Using session_id may not distribute data evenly.
✗ Incorrect
The user_id is commonly used as a sharding key to distribute data evenly across shards.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The modulo operator % is used to map the hash value to a shard number within the total shards.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using division returns float, causing index errors.
Multiplying or adding does not limit shard_id range.
✗ Incorrect
The modulo operator % ensures the shard_id is within the valid shard range.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < reverses the condition.
Assigning shard 1 instead of 0 for this range.
✗ Incorrect
Users with IDs less than 1000 go to shard 0.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of modulo for bucket assignment.
Mismatching key and iteration variable names.
✗ Incorrect
The comprehension maps each node to a bucket using modulo of its hash.