Challenge - 5 Problems
For Loop with Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of for loop with withIndex on a list
What is the output of this Kotlin code snippet?
val fruits = listOf("apple", "banana", "cherry")
for ((index, fruit) in fruits.withIndex()) {
println("$index: $fruit")
}Kotlin
val fruits = listOf("apple", "banana", "cherry") for ((index, fruit) in fruits.withIndex()) { println("$index: $fruit") }
Attempts:
2 left
💡 Hint
Remember that withIndex() provides both the index and the element in the loop.
✗ Incorrect
The withIndex() function returns an iterable of IndexedValue objects, each containing an index and the corresponding element. The loop destructures these into index and fruit, printing them in order.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in for loop with withIndex
Which option contains a syntax error in using withIndex() in a for loop?
Kotlin
val numbers = listOf(10, 20, 30) for ((i, num) in numbers.withIndex()) { println("Index $i has value $num") }
Attempts:
2 left
💡 Hint
Check how destructuring declarations are used in Kotlin for loops.
✗ Incorrect
Option D misses parentheses around the destructuring variables, which is required syntax in Kotlin for destructuring in for loops.
❓ optimization
advanced2:00remaining
Optimizing iteration with withIndex on large lists
Given a large list, which approach is more efficient to print index and value pairs?
Options:
A) Using withIndex() in a for loop
B) Using indices property and accessing elements by index
C) Using forEachIndexed function
D) Using a while loop with manual index increment
Options:
A) Using withIndex() in a for loop
B) Using indices property and accessing elements by index
C) Using forEachIndexed function
D) Using a while loop with manual index increment
Attempts:
2 left
💡 Hint
Consider readability and internal optimizations of Kotlin standard library functions.
✗ Incorrect
forEachIndexed is optimized and concise, often preferred for readability and performance over manual loops or withIndex in large collections.
🔧 Debug
advanced2:00remaining
Debug the unexpected output in for loop with withIndex
What causes this Kotlin code to print incorrect indices?
val items = listOf("a", "b", "c")
for ((index, item) in items.withIndex()) {
if (item == "b") continue
println("$index: $item")
}Attempts:
2 left
💡 Hint
Think about how continue affects loop iteration and index values.
✗ Incorrect
The continue statement skips the current iteration's print but the loop still increments index correctly. Indices remain consistent.
🧠 Conceptual
expert2:00remaining
Understanding withIndex behavior on different collections
Which statement about withIndex() in Kotlin is TRUE?
Attempts:
2 left
💡 Hint
Recall what types of collections implement Iterable and how withIndex works.
✗ Incorrect
withIndex() works on any Iterable, producing a sequence of IndexedValue objects with zero-based indices without modifying the original collection.