Array size and bounds in C - Time & Space Complexity
When working with arrays, it is important to understand how the size of the array affects the time it takes to access or process its elements.
We want to know how the number of operations changes as the array size grows.
Analyze the time complexity of the following code snippet.
#include <stdio.h>
int main() {
int arr[100];
int sum = 0;
for (int i = 0; i < 100; i++) {
sum += arr[i];
}
printf("Sum: %d\n", sum);
return 0;
}
This code sums all elements in an array of fixed size 100.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing each element of the array once to add it to sum.
- How many times: Exactly 100 times, once per element.
Since the array size is fixed at 100, the number of operations stays the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 (fixed) |
| 100 | 100 (fixed) |
| 1000 | 100 (fixed) |
Pattern observation: The operations do not grow with input size because the array size is fixed.
Time Complexity: O(1)
This means the time to run the code does not change as input size changes because the array size is fixed.
[X] Wrong: "Since there is a loop, the time must grow with input size."
[OK] Correct: Here, the loop runs a fixed number of times (100), so time does not grow with input size.
Understanding fixed-size arrays helps you explain how code behaves when input size is constant versus variable, a key skill in interviews.
"What if the array size was a variable n instead of fixed 100? How would the time complexity change?"
Practice
int arr[10]; in C?Solution
Step 1: Understand array size declaration
The arrayarris declared with size 10, meaning it has 10 elements.Step 2: Determine valid index range
In C, array indexing starts at 0, so valid indexes are from 0 up to size-1, which is 9.Final Answer:
0 to 9 -> Option AQuick Check:
Array indexes = 0 to size-1 = 0 to 9 [OK]
- Assuming array indexes start at 1
- Using size as the last index
- Confusing size with maximum index
Solution
Step 1: Recall C array declaration syntax
In C, arrays are declared using square brackets with the size inside, likeint arr[5];.Step 2: Check each option
int arr[5]; uses correct syntax. Options A and C use parentheses or braces which are invalid for arrays. int arr = 5; assigns an int, not an array.Final Answer:
int arr[5]; -> Option BQuick Check:
Array declaration uses square brackets [OK]
- Using parentheses instead of brackets
- Using braces instead of brackets
- Assigning a single value instead of declaring array
int arr[3] = {10, 20, 30};
printf("%d", arr[3]);Solution
Step 1: Analyze array size and indexing
The arrayarrhas size 3, valid indexes are 0, 1, 2. Accessingarr[3]is out of bounds.Step 2: Understand consequences of out-of-bounds access
Accessing outside array bounds in C causes undefined behavior. It may print garbage value, cause crash, or unpredictable output.Final Answer:
Undefined behavior (may print garbage or crash) -> Option AQuick Check:
Out-of-bounds access = undefined behavior [OK]
- Assuming arr[3] prints last element
- Expecting zero instead of garbage
- Thinking compiler catches out-of-bounds
int arr[4];
for(int i = 0; i <= 4; i++) {
arr[i] = i * 2;
}Solution
Step 1: Check loop bounds against array size
The array size is 4, so valid indexes are 0 to 3. The loop runs while i <= 4, which includes i=4, an invalid index.Step 2: Correct the loop condition
Changing the loop condition to i < 4 ensures indexes 0 to 3 are used, avoiding out-of-bounds access.Final Answer:
Loop condition should be i < 4, not i <= 4 -> Option CQuick Check:
Loop index must be less than array size [OK]
- Using <= instead of < in loop condition
- Assuming array size includes index equal to size
- Ignoring off-by-one errors
int nums[5] = {1, 2, 3, 4, 5};, which code snippet correctly sums all elements without causing out-of-bounds errors?Solution
Step 1: Understand array size and valid indexes
The arraynumshas size 5, so valid indexes are 0 to 4.Step 2: Evaluate each loop condition
for(int i = 0; i <= 5; i++) sum += nums[i]; uses i <= 5, which accesses nums[5] (out-of-bounds). for(int i = 1; i < 5; i++) sum += nums[i]; starts at 1, missing nums[0]. for(int i = 0; i < 5; i++) sum += nums[i]; uses i < 5, correctly iterating 0 to 4. for(int i = 0; i < 6; i++) sum += nums[i]; uses i < 6, accessing nums[5] (out-of-bounds).Final Answer:
for(int i = 0; i < 5; i++) sum += nums[i]; -> Option DQuick Check:
Loop from 0 to size-1 to avoid errors [OK]
- Using <= size causing out-of-bounds
- Starting loop at 1 missing first element
- Looping beyond array size
Ready to prove it?
Pass the test and this pattern moves unknown
