Bird
Raised Fist0
Swiftprogramming~20 mins

Collection slicing and indices in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Collection Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of slicing a Swift array with a range?

Consider the Swift array let numbers = [10, 20, 30, 40, 50]. What is the output of numbers[1..<4]?

Swift
let numbers = [10, 20, 30, 40, 50]
let slice = numbers[1..<4]
print(Array(slice))
A[10, 20, 30, 40]
B[20, 30, 40]
C[20, 30, 40, 50]
D[30, 40, 50]
Attempts:
2 left
💡 Hint

Remember that the range 1..<4 includes the start index but excludes the end index.

📝 Syntax
intermediate
2:00remaining
Which option correctly slices a Swift dictionary's keys?

Given let dict = ["a": 1, "b": 2, "c": 3, "d": 4], which code correctly slices the keys from index 1 to 3?

Swift
let dict = ["a": 1, "b": 2, "c": 3, "d": 4]
let keysSlice = ???
print(Array(keysSlice))
AArray(dict.keys)[1..<3]
Bdict.keys[1..<3]
Cdict.keys[1...3]
Ddict.keys[1,3]
Attempts:
2 left
💡 Hint

Dictionary keys are not directly indexable; you may need to convert them to an array first.

❓ optimization
advanced
2:00remaining
Which slicing method is most efficient for large Swift arrays?

You have a large array let bigArray = Array(1...1000000). Which slicing method avoids copying elements and is most efficient?

Swift
let bigArray = Array(1...1000000)
let slice = ???
AbigArray[100...199]
BArray(bigArray[100..<200])
CbigArray.prefix(200).suffix(100)
DbigArray[100..<200]
Attempts:
2 left
💡 Hint

Consider which methods return slices without copying the underlying elements.

🔧 Debug
advanced
2:00remaining
Why does this Swift slice cause a runtime error?

Given let arr = [1, 2, 3], why does let slice = arr[1..<5] cause a runtime error?

Swift
let arr = [1, 2, 3]
let slice = arr[1..<5]
print(Array(slice))
ASlicing with range operator is not allowed on arrays.
BThe lower bound 1 is invalid for slicing.
CThe upper bound 5 is out of range for the array indices.
DThe array must be converted to a slice before slicing.
Attempts:
2 left
💡 Hint

Check the valid index range for the array.

🧠 Conceptual
expert
2:00remaining
How does Swift's Collection slicing preserve indices?

When slicing a Swift collection, how are the indices of the slice related to the original collection?

AThe slice uses the same indices as the original collection, preserving index values.
BThe slice resets indices starting from zero regardless of original indices.
CThe slice uses random indices unrelated to the original collection.
DThe slice uses string keys as indices instead of integers.
Attempts:
2 left
💡 Hint

Think about how Swift's ArraySlice works with indices.

Practice

(1/5)
1. What does collection slicing in Swift allow you to do?
let numbers = [10, 20, 30, 40, 50]
For example, numbers[1...3] returns which part of the array?
easy
A. A part of the collection from index 1 to 3 inclusive
B. The entire collection reversed
C. Only the first element of the collection
D. An error because slicing is not allowed

Solution

  1. Step 1: Understand collection slicing syntax

    The syntax numbers[1...3] means take elements from index 1 to index 3, including both ends.
  2. Step 2: Identify the elements in the slice

    Indexes 1, 2, and 3 correspond to values 20, 30, and 40 respectively.
  3. Final Answer:

    A part of the collection from index 1 to 3 inclusive -> Option A
  4. Quick Check:

    Slice [1...3] = [20, 30, 40] [OK]
Hint: Remember slicing uses inclusive ranges [OK]
Common Mistakes:
  • Thinking slicing returns a single element
  • Assuming slicing reverses the collection
  • Believing slicing causes an error
2. Which of the following is the correct syntax to slice a Swift array named items from index 2 to 5 (inclusive)?
easy
A. items[2..<5]
B. items.slice(2, 5)
C. items[2...5]
D. items[2,5]

Solution

  1. Step 1: Understand Swift range operators

    The closed range operator ... includes both start and end indexes, so 2...5 means indexes 2, 3, 4, and 5.
  2. Step 2: Evaluate the slicing syntaxes

    items[2..<5] uses half-open range which excludes index 5. items.slice(2, 5) calls a non-existent method. items[2,5] is invalid syntax. items[2...5] is correct.
  3. Final Answer:

    items[2...5] -> Option C
  4. Quick Check:

    Closed range includes end index [OK]
Hint: Use ... for inclusive slicing in Swift [OK]
Common Mistakes:
  • Using half-open range when inclusive is needed
  • Using comma inside brackets incorrectly
  • Trying to call slice() method which doesn't exist
3. Given the Swift array:
let fruits = ["apple", "banana", "cherry", "date", "fig"]
What is the output of this code?
let slice = fruits[1..<4]
print(slice)
medium
A. ["banana", "cherry", "date"]
B. ["banana", "cherry", "date", "fig"]
C. ["banana", "cherry"]
D. Runtime error

Solution

  1. Step 1: Understand half-open range slicing

    The range 1..<4 includes indexes 1, 2, and 3 but excludes 4.
  2. Step 2: Identify elements at these indexes

    Indexes 1, 2, 3 correspond to "banana", "cherry", and "date".
  3. Final Answer:

    ["banana", "cherry", "date"] -> Option A
  4. Quick Check:

    Half-open range excludes last index [OK]
Hint: ..< excludes the upper bound index [OK]
Common Mistakes:
  • Including the element at index 4 mistakenly
  • Confusing half-open with closed range
  • Expecting a runtime error for valid slice
4. Consider this Swift code:
let letters = ["a", "b", "c", "d"]
let slice = letters[1...4]

What is the problem with this code?
medium
A. Slicing syntax is incorrect, should use parentheses
B. Index 4 is out of range causing a runtime error
C. Slice returns an empty array
D. No problem, code runs fine

Solution

  1. Step 1: Check array bounds

    The array has indexes 0 to 3. Index 4 does not exist.
  2. Step 2: Understand slicing with out-of-bounds index

    Using 1...4 tries to access index 4, which causes a runtime error.
  3. Final Answer:

    Index 4 is out of range causing a runtime error -> Option B
  4. Quick Check:

    Accessing invalid index causes error [OK]
Hint: Check array size before slicing [OK]
Common Mistakes:
  • Assuming slicing auto-adjusts out-of-range indexes
  • Thinking syntax is wrong instead of index error
  • Expecting empty slice instead of error
5. You have a Swift array:
let data = [5, 10, 15, 20, 25, 30]
You want to create a slice that includes every element except the first and last. Which code correctly creates this slice?
hard
A. let slice = data[1..
B. let slice = data[1...data.count-1]
C. let slice = data[1...data.count]
D. let slice = data[1..

Solution

  1. Step 1: Determine indexes to include

    Exclude first (index 0) and last (index 5) elements, so include indexes 1 to 4.
  2. Step 2: Use half-open range for slicing

    Using 1.. means indexes 1 up to but not including 5, which is indexes 1 to 4.
  3. Final Answer:

    let slice = data[1.. -> Option D
  4. Quick Check:

    Half-open range excludes last index [OK]
Hint: Use half-open range to exclude last index [OK]
Common Mistakes:
  • Using closed range which includes the last index
  • Using count directly as upper bound causing out-of-range
  • Confusing inclusive and exclusive ranges
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