Bird
Raised Fist0
Kafkadevops~20 mins

Leader election in Kafka - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kafka Leader Election Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kafka leader election log snippet?
Given the following Kafka broker log snippet during leader election, what is the final elected leader ID?
Kafka
[2024-06-01 10:00:00] Broker 1: Starting leader election for partition 0
[2024-06-01 10:00:01] Broker 2: Received vote request from Broker 1
[2024-06-01 10:00:02] Broker 3: Voted for Broker 1
[2024-06-01 10:00:03] Broker 2: Voted for Broker 1
[2024-06-01 10:00:04] Broker 1: Leader election complete. Leader is Broker 1
ALeader is Broker 2
BLeader is Broker 1
CLeader is Broker 3
DNo leader elected
Attempts:
2 left
💡 Hint
Look for the broker that received majority votes.
🧠 Conceptual
intermediate
1:30remaining
Which Kafka component is responsible for leader election?
In Kafka, which component manages the leader election process for partitions?
AKafka Broker
BKafka Producer
CKafka Consumer
DKafka Controller
Attempts:
2 left
💡 Hint
This component coordinates cluster metadata and elections.
🔧 Debug
advanced
2:30remaining
Why does this Kafka leader election fail?
Given this simplified code snippet for leader election, why does it fail to elect a leader? ```java Map votes = new HashMap<>(); votes.put(1, 1); votes.put(2, 1); if (votes.values().stream().max(Integer::compare).get() > votes.size() / 2) { System.out.println("Leader elected"); } else { System.out.println("No leader"); } ```
AThe condition uses integer division causing incorrect majority check
BThe votes map is empty
CThe max function returns null
DThe map keys are incorrect
Attempts:
2 left
💡 Hint
Check how division works with integers in Java.
📝 Syntax
advanced
2:00remaining
Which option correctly implements a Kafka leader election check in Python?
Select the Python code snippet that correctly checks if a candidate has majority votes in a leader election.
A
votes = {1: 2, 2: 1}
if max(votes.values()) &gt; len(votes) // 2:
    print('Leader elected')
B
votes = {1: 2, 2: 1}
if max(votes.values()) &gt; len(votes) / 2:
    print('Leader elected')
C
votes = {1: 2, 2: 1}
if max(votes.values()) &gt;= len(votes) / 2:
    print('Leader elected')
D
votes = {1: 2, 2: 1}
if max(votes.values()) &gt;= len(votes) // 2:
    print('Leader elected')
Attempts:
2 left
💡 Hint
Majority means strictly more than half using integer division.
🚀 Application
expert
3:00remaining
How many partitions have leaders after this Kafka cluster state update?
Given this Kafka cluster state JSON showing partition leaders, how many partitions currently have a leader assigned? ```json { "partitions": [ {"partition": 0, "leader": 1}, {"partition": 1, "leader": -1}, {"partition": 2, "leader": 2}, {"partition": 3, "leader": -1}, {"partition": 4, "leader": 3} ] } ```
A4
B2
C3
D5
Attempts:
2 left
💡 Hint
Leader -1 means no leader assigned.

Practice

(1/5)
1. What is the main purpose of leader election in Kafka?
easy
A. To backup data to external storage
B. To balance the load evenly among all brokers
C. To choose one broker to lead and coordinate tasks
D. To monitor client connections

Solution

  1. Step 1: Understand leader election role

    Leader election picks one broker as the leader to manage partition tasks.
  2. Step 2: Compare options

    Only To choose one broker to lead and coordinate tasks describes choosing a broker to lead and coordinate tasks, which matches Kafka's leader election purpose.
  3. Final Answer:

    To choose one broker to lead and coordinate tasks -> Option C
  4. Quick Check:

    Leader election = choose leader broker [OK]
Hint: Leader election picks one leader broker, not all brokers [OK]
Common Mistakes:
  • Confusing leader election with load balancing
  • Thinking leader election backs up data
  • Assuming leader election monitors clients
2. Which of the following is the correct way to configure Kafka broker IDs for leader election?
easy
A. broker.id=unique_integer_per_broker
B. broker.id=localhost
C. broker.id=auto_assign
D. broker.id=0.0.0.0

Solution

  1. Step 1: Identify broker ID type

    Kafka requires each broker to have a unique integer ID for leader election.
  2. Step 2: Check options

    Only broker.id=unique_integer_per_broker uses a unique integer, while others use invalid or non-integer values.
  3. Final Answer:

    broker.id=unique_integer_per_broker -> Option A
  4. Quick Check:

    Broker ID must be unique integer [OK]
Hint: Broker ID must be a unique integer, not a string or IP [OK]
Common Mistakes:
  • Using hostnames or IPs as broker IDs
  • Setting broker.id to zero or non-unique values
  • Assuming auto assignment works without config
3. Given the following Kafka cluster state, which broker will be the leader for partition 0?
Broker 1: ISR = [1, 2, 3]
Broker 2: ISR = [1, 2, 3]
Broker 3: ISR = [1, 2, 3]
Current leader: Broker 2
Broker 2 fails
medium
A. Broker 2
B. Broker 1
C. Broker 3
D. No leader assigned

Solution

  1. Step 1: Understand ISR and leader failover

    ISR (In-Sync Replicas) are eligible leaders. When current leader fails, next ISR broker becomes leader.
  2. Step 2: Identify next leader after Broker 2 fails

    Broker 1 and 3 are in ISR. Kafka picks the lowest broker ID in ISR as new leader, so Broker 1.
  3. Final Answer:

    Broker 1 -> Option B
  4. Quick Check:

    Next leader = lowest ISR broker after failure [OK]
Hint: Leader election picks lowest ISR broker if current leader fails [OK]
Common Mistakes:
  • Choosing failed broker as leader
  • Picking broker outside ISR
  • Assuming no leader assigned after failure
4. You have a Kafka cluster where leader election is not working properly. Which of the following is the most likely cause?
medium
A. Each broker has a unique broker.id
B. Zookeeper is running and connected
C. Partitions have multiple replicas
D. All brokers have the same broker.id

Solution

  1. Step 1: Identify leader election requirements

    Leader election requires each broker to have a unique broker.id to distinguish them.
  2. Step 2: Analyze causes

    If all brokers share the same broker.id, leader election cannot assign a unique leader, causing failure.
  3. Final Answer:

    All brokers have the same broker.id -> Option D
  4. Quick Check:

    Unique broker IDs needed for leader election [OK]
Hint: Check broker IDs; duplicates break leader election [OK]
Common Mistakes:
  • Assuming Zookeeper connection causes failure
  • Thinking multiple replicas cause election failure
  • Ignoring broker.id uniqueness
5. In a Kafka cluster using KRaft mode (without Zookeeper), how does leader election differ from the traditional Zookeeper-based approach?
hard
A. KRaft uses an internal quorum of controllers to elect leaders without external Zookeeper
B. KRaft requires manual leader assignment by the admin
C. KRaft disables leader election and uses round-robin partition assignment
D. KRaft uses Zookeeper but with a different election algorithm

Solution

  1. Step 1: Understand KRaft mode leader election

    KRaft mode replaces Zookeeper with an internal controller quorum that manages leader election.
  2. Step 2: Compare options

    Only KRaft uses an internal quorum of controllers to elect leaders without external Zookeeper correctly states KRaft uses internal quorum without external Zookeeper for leader election.
  3. Final Answer:

    KRaft uses an internal quorum of controllers to elect leaders without external Zookeeper -> Option A
  4. Quick Check:

    KRaft = internal quorum leader election [OK]
Hint: KRaft replaces Zookeeper with internal controller quorum [OK]
Common Mistakes:
  • Thinking KRaft disables leader election
  • Assuming manual leader assignment in KRaft
  • Believing KRaft still uses external Zookeeper
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard