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 FundamentalsLoad balancerspredict6 / 105
+75 XP
Task
📝 **Task:** Predict the four lines the snippet prints. Two algorithms × probe requests; trace each pick. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

apps = ["app1", "app2", "app3"]

# Round-robin: cycle through apps in order.
class RoundRobin:
    def __init__(self, apps):
        self.apps = apps
        self.i = 0
    def pick(self, ip):
        choice = self.apps[self.i % len(self.apps)]
        self.i += 1
        return choice

# IP-hash: same ip ALWAYS lands on the same app — sticky sessions.
# We use sum(ord(c)) instead of hash() so the answer is reproducible
# across Python versions (CPython hash() is randomised per process).
class IPHash:
    def __init__(self, apps):
        self.apps = apps
    def pick(self, ip):
        return self.apps[sum(ord(c) for c in ip) % len(self.apps)]

rr = RoundRobin(apps)
print(rr.pick("10.0.0.1"))    # request 1 — cycle position 0
print(rr.pick("10.0.0.2"))    # request 2 — cycle position 1

ih = IPHash(apps)
print(ih.pick("10.0.0.5"))    # same IP twice — should pin
print(ih.pick("10.0.0.5"))

# 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…