Array slicing in Bash Scripting - Time & Space Complexity
We want to understand how the time it takes to slice an array grows as the array gets bigger.
How does the number of elements we take affect the work done?
Analyze the time complexity of the following code snippet.
arr=(one two three four five six seven eight nine ten)
slice=("${arr[@]:2:4}")
echo "${slice[@]}"
This code takes a slice of 4 elements starting from index 2 of the array and prints them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying each element from the original array slice to the new slice.
- How many times: Once for each element in the slice (4 times in this example).
As the number of elements you want to slice grows, the work grows roughly the same amount.
| Input Size (slice length) | Approx. Operations |
|---|---|
| 10 | 10 copies |
| 100 | 100 copies |
| 1000 | 1000 copies |
Pattern observation: The time grows linearly with the number of elements sliced.
Time Complexity: O(k)
This means the time depends directly on how many elements you slice, not on the total array size.
[X] Wrong: "Slicing an array always takes time proportional to the whole array size."
[OK] Correct: Actually, only the number of elements you copy matters, not the entire array length.
Knowing how slicing scales helps you write efficient scripts and explain your code clearly in interviews.
"What if we slice the array without specifying length (till the end)? How would the time complexity change?"
Practice
${array[@]:start:length}?Solution
Step 1: Understand array slicing syntax
In Bash,${array[@]:start:length}extracts elements from the array starting at indexstartand takeslengthelements.Step 2: Compare options with this behavior
Only Extracts a part of the array starting atstartindex withlengthelements correctly describes this extraction. Other options describe unrelated operations.Final Answer:
Extracts a part of the array starting at start index with length elements -> Option CQuick Check:
Array slicing = Extract part [OK]
- Confusing slicing with deleting elements
- Thinking slicing sorts or reverses the array
- Mixing up start index and length meaning
arr?Solution
Step 1: Recall correct array slicing syntax
In Bash, to slice an array, use${array[@]:start:length}. The@is needed to refer to all elements.Step 2: Check each option
${arr[@]:2:3}matches the correct syntax:${arr[@]:2:3}.${arr:2:3}misses@, which is required for arrays. Options B and D use invalid syntax.Final Answer:
${arr[@]:2:3} -> Option BQuick Check:
Use ${array[@]:start:length} for slicing [OK]
- Omitting @ when slicing arrays
- Using square brackets with colon syntax
- Trying to slice with invalid range notation
arr=(a b c d e), what is the output of echo ${arr[@]:1:3}?Solution
Step 1: Identify the array and slice parameters
The array is(a b c d e). The slice starts at index 1 and takes 3 elements.Step 2: Extract elements from index 1 to 3
Index 0 = a, 1 = b, 2 = c, 3 = d, 4 = e. Taking 3 elements from index 1 gives: b, c, d.Final Answer:
b c d -> Option DQuick Check:
Slice from 1 length 3 = b c d [OK]
- Starting count at 1 instead of 0
- Taking wrong number of elements
- Confusing output with full array
arr=(x y z)
echo ${arr:1:2}Solution
Step 1: Check slicing syntax correctness
The code uses${arr:1:2}without@. For arrays,@is needed to slice multiple elements.Step 2: Understand the effect of missing @
Without@, Bash uses the first elementxas a scalar string and slices it from index 1 length 2. Sincexhas length 1, it outputs an empty string.Final Answer:
It will output an empty string -> Option AQuick Check:
Missing @ slices first elem as string -> empty [OK]
- Omitting @ and expecting array slice
- Assuming no error but wrong output
- Confusing string and array slicing syntax
arr=(1 2 3 4 5). You want to extract the last 3 elements using array slicing. Which command will do this correctly?Solution
Step 1: Understand negative index slicing in Bash
Using a negative start index slices from the end.${arr[@]:-3}extracts from 3rd from end to the end (last 3 elements).Step 2: Check each option
${arr[@]: -3:3}has space after:causing bad substitution.${arr[@]:3:3}slices from index 3 length 3 but only 2 elements remain (4 5).${arr[@]:3}slices from 3 to end (4 5). Only${arr[@]:-3}gets exactly the last 3: 3 4 5.Final Answer:
echo ${arr[@]:-3} -> Option AQuick Check:
Negative start index slices from end [OK]
- Adding space after ':' before negative offset
- Using slice length larger than remaining elements
- Not knowing negative indices slice from array end
Ready to prove it?
Pass the test and this pattern moves unknown
