Challenge - 5 Problems
Kafka Partition Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kafka partition assignment code snippet?
Consider a Kafka consumer group with 3 consumers and a topic with 6 partitions. The default RangeAssignor is used. What partitions will consumer 2 (index 1) be assigned?
Kafka
consumers = ['consumer0', 'consumer1', 'consumer2'] partitions = list(range(6)) # RangeAssignor assigns partitions in contiguous blocks # Each consumer gets partitions in a range assignments = {} num_consumers = len(consumers) num_partitions = len(partitions) partitions_per_consumer = num_partitions // num_consumers remainder = num_partitions % num_consumers start = 0 for i, consumer in enumerate(consumers): extra = 1 if i < remainder else 0 end = start + partitions_per_consumer + extra assignments[consumer] = partitions[start:end] start = end print(assignments['consumer1'])
Attempts:
2 left
💡 Hint
Remember that RangeAssignor distributes partitions in contiguous blocks and the first consumers get the extra partitions if partitions don't divide evenly.
✗ Incorrect
With 6 partitions and 3 consumers, each consumer gets 2 partitions. Since 6 % 3 = 0, no extra partitions are assigned. The partitions are split as: consumer0 -> [0,1], consumer1 -> [2,3], consumer2 -> [4,5]. So consumer1 gets partitions [2,3]. The correct answer is C: [2,3].
❓ Predict Output
intermediate2:00remaining
What error does this Kafka custom partition assignment code raise?
This code tries to assign partitions manually but has a bug. What error will it raise?
Kafka
consumers = ['c1', 'c2'] partitions = [0, 1, 2] assignments = {} for i, consumer in enumerate(consumers): assignments[consumer] = partitions[i*2:(i+1)*2] print(assignments)
Attempts:
2 left
💡 Hint
Check how slicing works in Python when the slice end index exceeds list length.
✗ Incorrect
Python slicing does not raise IndexError if the slice end index is beyond the list length. It just returns the available elements. So consumer 'c2' gets partition [2]. No error occurs.
❓ Predict Output
advanced2:00remaining
What is the output of this Kafka StickyAssignor partition assignment simulation?
Given 4 consumers and 7 partitions, the StickyAssignor tries to balance partitions evenly. After one rebalance, what partitions does consumer 'c3' get?
Kafka
consumers = ['c0', 'c1', 'c2', 'c3'] partitions = list(range(7)) # StickyAssignor tries to assign partitions evenly # Each consumer gets either 1 or 2 partitions assignments = { 'c0': [0, 1], 'c1': [2, 3], 'c2': [4, 5], 'c3': [6] } print(assignments['c3'])
Attempts:
2 left
💡 Hint
StickyAssignor tries to keep previous assignments and balance partitions as evenly as possible.
✗ Incorrect
With 7 partitions and 4 consumers, three consumers get 2 partitions and one gets 1 partition. Here, 'c3' gets only partition 6.
🧠 Conceptual
advanced1:30remaining
Which option best describes Kafka's CooperativeStickyAssignor behavior?
Kafka's CooperativeStickyAssignor allows incremental rebalancing. Which statement is true about its partition assignment?
Attempts:
2 left
💡 Hint
Think about how incremental rebalancing helps reduce disruption.
✗ Incorrect
CooperativeStickyAssignor moves only the partitions that must be reassigned, reducing the number of partitions moved during rebalances.
❓ Predict Output
expert2:30remaining
What is the output of this Kafka partition assignment code with a custom assignor?
This code simulates a custom assignor that assigns partitions to consumers in a round-robin fashion. What is the assignment for consumer 'c1'?
Kafka
consumers = ['c0', 'c1', 'c2'] partitions = list(range(8)) assignments = {c: [] for c in consumers} for i, p in enumerate(partitions): consumer = consumers[i % len(consumers)] assignments[consumer].append(p) print(assignments['c1'])
Attempts:
2 left
💡 Hint
Round-robin assigns partitions one by one cycling through consumers.
✗ Incorrect
Partitions assigned to 'c1' are those where partition index modulo 3 equals 1: partitions 1, 4, and 7.