0
0
Kafkadevops~20 mins

Offset management in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Offset 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 consumer offset commit code?
Consider the following Kafka consumer code snippet that commits offsets synchronously. What will be printed if the commit succeeds?
Kafka
consumer.subscribe(['my_topic'])
try:
    records = consumer.poll(timeout_ms=1000)
    for record in records:
        print(f"Received: {record.value}")
    consumer.commit()
    print("Offsets committed successfully")
except Exception as e:
    print(f"Commit failed: {e}")
ANo output, consumer.poll returns empty
BCommit failed: OffsetOutOfRangeException
CReceived: <message_value>\nOffsets committed successfully
DOffsets committed successfully
Attempts:
2 left
💡 Hint
The commit() call happens after messages are received and printed.
🧠 Conceptual
intermediate
1:30remaining
Which offset reset policy causes the consumer to start from the earliest offset when no committed offset exists?
Kafka consumers can be configured with an offset reset policy. Which of the following values for 'auto.offset.reset' causes the consumer to read from the earliest offset if no committed offset is found?
A"auto"
B"latest"
C"none"
D"earliest"
Attempts:
2 left
💡 Hint
Think about starting from the beginning of the topic.
Predict Output
advanced
2:00remaining
What error does this Kafka consumer code raise?
Given the following code snippet, what error will be raised when running it?
Kafka
consumer.assign([TopicPartition('my_topic', 0)])
consumer.seek(TopicPartition('my_topic', 0), 100)
records = consumer.poll(timeout_ms=1000)
for record in records:
    print(record.value)
ATimeoutException: Poll timed out
BNo error, prints messages starting from offset 100
COffsetOutOfRangeException
DIllegalStateException: Consumer not subscribed to topic
Attempts:
2 left
💡 Hint
Assigning partitions manually allows seeking to offsets.
Predict Output
advanced
2:00remaining
What is the output of this asynchronous offset commit example?
Examine this Kafka consumer code that commits offsets asynchronously with a callback. What will be printed if the commit fails?
Kafka
def on_commit(err, partitions):
    if err:
        print(f"Commit failed for partitions: {partitions}")
    else:
        print("Commit succeeded")

consumer.subscribe(['my_topic'])
consumer.poll(timeout_ms=1000)
consumer.commit_async(callback=on_commit)
ACommit failed for partitions: [TopicPartition(topic='my_topic', partition=0)]
BCommit succeeded
CNo output, commit_async is non-blocking
DSyntaxError due to callback parameter
Attempts:
2 left
💡 Hint
The callback prints a message only if there is an error.
🧠 Conceptual
expert
2:30remaining
How many committed offsets are stored if a consumer group consumes 3 partitions and commits offsets only for 2 partitions?
A Kafka consumer group reads from 3 partitions of a topic. It commits offsets only for 2 partitions and never commits for the third. How many committed offsets will Kafka store for this group?
A2 committed offsets, only for the partitions committed
B1 committed offset, the latest among all partitions
C3 committed offsets, one per partition
D0 committed offsets, because all partitions must be committed together
Attempts:
2 left
💡 Hint
Offsets are stored per partition independently.