Challenge - 5 Problems
Array Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of reversing an array using two-pointer swap
What is the output of the following Go code that reverses an array using two pointers?
DSA Go
package main import "fmt" func main() { arr := []int{1, 2, 3, 4, 5} left, right := 0, len(arr)-1 for left < right { arr[left], arr[right] = arr[right], arr[left] left++ right-- } fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Think about how swapping elements from both ends moves towards the center.
✗ Incorrect
The code swaps elements from the start and end moving inward, reversing the array completely.
❓ Predict Output
intermediateOutput of reversing an array using a new array
What is the output of this Go code that reverses an array by creating a new reversed array?
DSA Go
package main import "fmt" func main() { arr := []int{10, 20, 30, 40} rev := make([]int, len(arr)) for i := range arr { rev[len(arr)-1-i] = arr[i] } fmt.Println(rev) }
Attempts:
2 left
💡 Hint
Indexing from the end of the new array places elements in reverse order.
✗ Incorrect
The code copies elements from the original array into the new array starting from the last index backwards.
🔧 Debug
advancedIdentify the error in this array reversal code
What error does this Go code produce when trying to reverse an array in place?
DSA Go
package main import "fmt" func main() { arr := []int{1, 2, 3, 4} for i := 0; i < len(arr)/2; i++ { arr[i], arr[len(arr)-1-i] = arr[len(arr)-1-i], arr[i] } fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The loop runs one step too many, causing indices 1 and 2 to be swapped twice, leading to incorrect reversal.
🧠 Conceptual
advancedTime complexity of array reversal methods
What is the time complexity of reversing an array of size n using the two-pointer swap method?
Attempts:
2 left
💡 Hint
Consider how many swaps happen relative to the array size.
✗ Incorrect
Each element is swapped at most once, so the time grows linearly with the array size.
🚀 Application
expertReversing a linked list using array reversal logic
Given a singly linked list, which approach below best mimics the array reversal two-pointer swap method to reverse the list?
Attempts:
2 left
💡 Hint
Think about how array reversal swaps values at two ends simultaneously.
✗ Incorrect
Using two pointers from start and end swapping values mimics the array two-pointer swap method, though it requires accessing nodes by index.
