Array Access and Update at Index in DSA C - Time & Space Complexity
We want to understand how fast we can get or change a value in an array at a certain position.
The question is: How does the time to access or update an element change as the array grows?
Analyze the time complexity of the following code snippet.
int getValue(int arr[], int index) {
return arr[index];
}
void setValue(int arr[], int index, int value) {
arr[index] = value;
}
This code gets or sets a value at a specific index in an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Direct access to an array element by index.
- How many times: Exactly once per call, no loops or recursion involved.
Accessing or updating an element takes the same amount of time no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant even if the array size grows.
Time Complexity: O(1)
This means accessing or updating an element takes the same short time no matter how big the array is.
[X] Wrong: "Accessing an element takes longer if the array is bigger because it has to look through all elements."
[OK] Correct: Arrays allow direct access by index, so the computer jumps straight to the spot without checking others.
Knowing that array access and update are very fast helps you choose the right data structure and explain your choices clearly in interviews.
"What if we used a linked list instead of an array? How would the time complexity for access and update change?"
Practice
arr[2] do in an array named arr?Solution
Step 1: Understand array indexing
Arrays use zero-based indexing, so index 2 means the third element.Step 2: Access element at index 2
The expressionarr[2]retrieves the value stored at the third position in the array.Final Answer:
Accesses the element at index 2 of the array -> Option BQuick Check:
arr[2] means third element access [OK]
- Thinking index starts at 1 instead of 0
- Confusing access with update or delete
- Assuming arr[2] changes the array
arr to 10 in C?Solution
Step 1: Identify correct array indexing syntax
In C, arrays use square brackets with zero-based indexing, so the 4th element is at index 3.Step 2: Use assignment operator correctly
To update, use single equals=, soarr[3] = 10;is correct.Final Answer:
arr[3] = 10; -> Option AQuick Check:
Use arr[index] = value; with zero-based index [OK]
- Using parentheses or braces instead of brackets
- Using double equals (==) instead of assignment (=)
- Using wrong index number for 4th element
int arr[5] = {1, 2, 3, 4, 5};
arr[1] = arr[3] + 2;
printf("%d", arr[1]);Solution
Step 1: Evaluate the expression arr[3] + 2
arr[3] is the 4th element, which is 4. Adding 2 gives 6.Step 2: Assign and print arr[1]
arr[1] is updated to 6, then printed, so output is 6.Final Answer:
6 -> Option AQuick Check:
arr[3]=4 + 2 = 6 printed [OK]
- Confusing index 1 with index 3 values
- Adding incorrectly or forgetting zero-based index
- Printing wrong array element
int arr[3] = {5, 10, 15};
arr[3] = 20;
printf("%d", arr[3]);Solution
Step 1: Check array size and valid indices
Array size is 3, valid indices are 0, 1, 2. Index 3 is out of bounds.Step 2: Understand consequences of out of bounds access
Accessing or assigning arr[3] causes undefined behavior or runtime error.Final Answer:
Array index out of bounds error -> Option CQuick Check:
Index 3 invalid for size 3 array [OK]
- Using index equal to array size
- Ignoring zero-based indexing
- Assuming no error for out of bounds
arr of size 6 initialized as {2, 4, 6, 8, 10, 12}, which code snippet correctly doubles the value at index 4 and then prints the entire array?Solution
Step 1: Correctly update value at index 4
Index 4 is the 5th element. Doubling means multiply by 2. Botharr[4] = arr[4] * 2;andarr[4] *= 2;work, but check loop next.Step 2: Print entire array correctly
Array size is 6, so loop must run from 0 to 5 (i<6). arr[4] = arr[4] * 2; for(int i=0; i<6; i++) printf("%d ", arr[i]); uses correct loop range and prints all elements.Final Answer:
arr[4] = arr[4] * 2; for(int i=0; i<6; i++) printf("%d ", arr[i]); -> Option DQuick Check:
Update index 4, loop 0 to 5 to print all [OK]
- Using wrong index for update
- Looping beyond array size causing errors
- Printing partial array missing elements
Ready to prove it?
Pass the test and this pattern moves unknown
