Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after deleting the last element from the array?
Consider the following Go code that deletes the last element from an array (slice). What will be printed?
DSA Go
package main import "fmt" func main() { arr := []int{10, 20, 30, 40, 50} arr = arr[:len(arr)-1] fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Remember that slicing with arr[:len(arr)-1] removes the last element.
✗ Incorrect
The code slices the array to exclude the last element. The original array has 5 elements, and slicing up to len(arr)-1 removes the last one.
❓ Predict Output
intermediateWhat happens if we delete the last element from an empty slice?
What will happen when the following Go code runs?
DSA Go
package main import "fmt" func main() { arr := []int{} arr = arr[:len(arr)-1] fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Check what len(arr) is when arr is empty and what happens when you subtract 1.
✗ Incorrect
Since the slice is empty, len(arr) is 0. Trying to slice arr[:len(arr)-1] means arr[:-1], which is invalid and causes a runtime error.
🔧 Debug
advancedWhy does this code cause a runtime panic when deleting the last element?
Identify the cause of the runtime panic in this Go code snippet.
DSA Go
package main import "fmt" func main() { arr := []int{1} arr = arr[:len(arr)-2] fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Check the value of len(arr)-2 when arr has only one element.
✗ Incorrect
len(arr) is 1, so len(arr)-2 equals -1, which is an invalid slice index causing a runtime panic.
🧠 Conceptual
advancedWhat is the time complexity of deleting the last element from a Go slice?
Consider deleting the last element from a slice in Go using slicing. What is the time complexity of this operation?
Attempts:
2 left
💡 Hint
Think about whether elements need to be shifted when slicing.
✗ Incorrect
Deleting the last element by slicing does not move elements; it just changes the slice length, so it is constant time.
🚀 Application
expertAfter deleting the last element multiple times, what is the final slice?
Given the following Go code, what is the final printed slice?
DSA Go
package main import "fmt" func main() { arr := []int{5, 10, 15, 20, 25} for i := 0; i < 3; i++ { arr = arr[:len(arr)-1] } fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Each iteration removes one element from the end. Count carefully.
✗ Incorrect
The loop runs 3 times, each time removing the last element. Starting with 5 elements, after 3 removals, 2 remain.
