Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSystem Design for Python JuniorsModule 1 · System Design FundamentalsSharding (horizontal partitioning)predict8 / 105
+100 XP
Task
📝 **Task:** Predict the 5-line output: first 4 lines show shard:count for shards 0..3 in the 4-shard layout, then one line of comma-joined moves after re-sharding to 5 shards. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

from collections import Counter

USERS = ["u1", "u2", "u3", "u4", "u5", "u6", "u7", "u8"]

def shard_for(uid, n):
    return sum(ord(c) for c in uid) % n

def distribution(users, n):
    return Counter(shard_for(u, n) for u in users)

# 4-shard layout — print one line per shard.
d4 = distribution(USERS, 4)
for s in range(4):
    print(f"shard{s}:{d4[s]}")

# Re-shard to 5: how many users HOP to a different bucket?
moved = [u for u in USERS if shard_for(u, 4) != shard_for(u, 5)]
print("moved:" + ",".join(moved))

# What does this print? Type your prediction.

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…