Array Access and Update at Index in DSA Typescript - Time & Space Complexity
We want to understand how fast we can get or change a value in an array by its 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.
const arr = [10, 20, 30, 40, 50];
// Access element at index 2
const value = arr[2];
// Update element at index 4
arr[4] = 60;
This code gets the value at position 2 and changes the value at position 4 in the array.
Here, there are no loops or repeated steps.
- Primary operation: Direct access or update at a specific index.
- How many times: Exactly once per access or update.
Accessing or updating an element takes the same 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 grows larger.
Time Complexity: O(1)
This means accessing or updating an element by index takes the same small amount of 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 items."
[OK] Correct: Arrays let us jump directly to any position, so the time does not depend on the array size.
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 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 element stored at the third position.Final Answer:
Accesses the element at index 2 of the array -> Option AQuick Check:
arr[2] means get element at index 2 [OK]
- Thinking index 2 adds or removes elements
- Confusing index with length
- Assuming indexing starts at 1
nums to 10?Solution
Step 1: Recall array element update syntax
In TypeScript, to update an element at a specific index, usearray[index] = value;.Step 2: Apply to given array
So,nums[1] = 10;correctly updates the element at index 1 to 10.Final Answer:
nums[1] = 10; -> Option DQuick Check:
Use bracket notation with assignment for update [OK]
- Using non-existent methods like update(), set(), add()
- Confusing update syntax with other languages
- Trying to use push() to update element
const arr = [5, 10, 15]; arr[1] = 20; console.log(arr);
Solution
Step 1: Analyze initial array and update
The array starts as [5, 10, 15]. The statementarr[1] = 20;changes the element at index 1 from 10 to 20.Step 2: Determine final array state
After update, the array becomes [5, 20, 15]. The console.log prints this updated array.Final Answer:
[5, 20, 15] -> Option CQuick Check:
Update index 1 to 20 changes array to [5, 20, 15] [OK]
- Not updating the element, so output stays same
- Mixing up indexes and values
- Assuming update changes order
let fruits = ['apple', 'banana', 'cherry']; fruits(1) = 'orange';
Solution
Step 1: Check array element update syntax
Array elements must be accessed or updated using square brackets[], not parentheses().Step 2: Identify the error in code
The code usesfruits(1) = 'orange';which is invalid syntax. It should befruits[1] = 'orange';.Final Answer:
Using parentheses instead of square brackets for indexing -> Option AQuick Check:
Use square brackets [] to access/update array elements [OK]
- Using parentheses instead of brackets
- Thinking let vs const causes error here
- Ignoring syntax errors due to missing semicolon
let scores = [10, 20, 30, 40, 50];, which code snippet correctly doubles the value at index 3 and then prints the updated array?Solution
Step 1: Understand how to double element at index 3
Index 3 corresponds to the fourth element (value 40). Doubling means multiplying by 2.Step 2: Check each option for correct syntax and effect
scores[3] = scores[3] * 2; console.log(scores); uses explicit multiplication and assignment, which works. scores[3] *= 2; console.log(scores); uses shorthand*=operator, also correct. scores.update(3, scores[3] * 2); console.log(scores); uses a non-existent methodupdate(). scores[3] = scores[3] + scores[3]; console.log(scores); adds the element to itself, which also doubles it.Step 3: Identify the best concise and correct option
scores[3] *= 2; console.log(scores); is the most concise and idiomatic way to double the value at index 3.Final Answer:
scores[3] *= 2; console.log(scores); -> Option BQuick Check:
Use *= operator to double value at index 3 [OK]
- Using non-existent update() method
- Forgetting to print the array after update
- Using addition instead of multiplication
Ready to prove it?
Pass the test and this pattern moves unknown
