Array traversal means visiting each element in an array one by one, usually from start to end, to perform some operation.
Click to reveal answer
beginner
How do you traverse an array using a for loop in TypeScript?
Use a for loop with an index starting at 0 and ending at array.length - 1. Access elements with array[index].
Click to reveal answer
intermediate
What is the difference between for loop and for...of loop for array traversal?
for loop uses index to access elements, giving control over position. for...of loop directly accesses elements without index.
Click to reveal answer
intermediate
Explain the two-pointer technique in array traversal.
Two pointers start at different positions (like start and end) and move towards each other or in some pattern to solve problems efficiently.
Click to reveal answer
beginner
Why is it important to avoid modifying the array length during traversal?
Changing array length while traversing can skip elements or cause errors because the loop count or indexes become inconsistent.
Click to reveal answer
Which loop is best when you need the index of elements during array traversal?
Awhile loop without index
Bfor loop
Cfor...of loop
Ddo...while loop without index
✗ Incorrect
The for loop uses an index variable, so you can access both the element and its position.
What does the two-pointer technique usually involve?
ASorting the array before traversal
BOne pointer moving twice as fast as the other
CUsing recursion to traverse arrays
DTwo pointers moving in opposite directions
✗ Incorrect
Two pointers often start at opposite ends and move towards each other to solve problems efficiently.
What happens if you increase the array length inside a for loop traversing the array?
AThe loop may run infinitely or skip elements
BThe loop ends immediately
CNothing changes
DThe array is copied automatically
✗ Incorrect
Changing length during traversal can cause unexpected behavior like infinite loops or skipped elements.
Which of these is a valid way to traverse all elements in an array in TypeScript?
AAll of the above
Bfor (let element of array)
Carray.forEach(element => ...)
Dfor (let i = 0; i < array.length; i++)
✗ Incorrect
All these methods correctly traverse all elements in an array.
When is using a for...of loop preferred over a for loop?
AWhen you want to skip elements
BWhen you need to modify the array length
CWhen you only need the element, not the index
DWhen you want to traverse backwards
✗ Incorrect
for...of loop directly accesses elements without indexes, good when index is not needed.
Describe how you would traverse an array to print all its elements in order.
Think about starting from the first element and moving to the last.
You got /3 concepts.
Explain the two-pointer technique and give an example of when it might be useful.
Imagine two fingers moving from both ends of a line towards the center.
You got /3 concepts.
Practice
(1/5)
1. What does the following TypeScript code do? for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }
easy
A. Prints each element of the array from start to end
B. Prints the array elements in reverse order
C. Prints only the first element of the array
D. Prints the length of the array
Solution
Step 1: Understand the loop range
The loop starts at 0 and runs while i is less than arr.length, so it covers all valid indexes.
Step 2: Analyze the console.log statement
Each iteration prints the element at index i, so all elements are printed in order.
Final Answer:
Prints each element of the array from start to end -> Option A
Quick Check:
Standard forward traversal prints all elements [OK]
Hint: For loop from 0 to length prints array forward [OK]
Common Mistakes:
Thinking it prints in reverse order
Assuming it prints only one element
Confusing index with value
2. Which of the following is the correct syntax to traverse an array arr using a for-of loop in TypeScript?
easy
A. for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }
B. for (let item of arr) { console.log(item); }
C. for (let i in arr) { console.log(arr[i]); }
D. for (let i = arr.length; i >= 0; i--) { console.log(arr[i]); }
Solution
Step 1: Identify for-of loop syntax
The for-of loop syntax is: for (let element of array) { ... }
Step 2: Match with options
for (let item of arr) { console.log(item); } matches this syntax and prints each element directly.
Final Answer:
for (let item of arr) { console.log(item); } -> Option B
Quick Check:
for-of loops use 'of' keyword to iterate elements [OK]
Hint: for-of loops use 'of' keyword to get elements directly [OK]
Common Mistakes:
Using for-in to get elements instead of indexes
Confusing for-of with traditional for loop syntax
Using wrong loop conditions
3. What will be the output of the following code?
const arr = [10, 20, 30];
let result = [];
for (let i = arr.length - 1; i >= 0; i--) {
result.push(arr[i]);
}
console.log(result);
medium
A. SyntaxError
B. [10, 20, 30]
C. [undefined, 30, 20]
D. [30, 20, 10]
Solution
Step 1: Understand the loop direction
The loop starts at index 2 (arr.length - 1) and decrements to 0, traversing the array backward.
Step 2: Track elements pushed to result
Elements pushed in order: arr[2] = 30, arr[1] = 20, arr[0] = 10, so result = [30, 20, 10].
Final Answer:
[30, 20, 10] -> Option D
Quick Check:
Reverse traversal collects elements backward [OK]
Hint: Loop from length-1 down to 0 reverses array [OK]
Common Mistakes:
Assuming forward order output
Confusing index bounds causing undefined
Thinking it causes syntax error
4. Identify the error in the following code snippet that attempts to print all elements of arr:
const arr = [1, 2, 3];
for (let i = 0; i <= arr.length; i++) {
console.log(arr[i]);
}
medium
A. The loop condition should be i < arr.length, not i <= arr.length
B. The variable i should start from 1, not 0
C. The array should be traversed using for-of loop only
D. There is no error; code works fine
Solution
Step 1: Check loop condition
The loop runs while i <= arr.length, which means i can be equal to arr.length (3), an invalid index.
Step 2: Understand array indexing
Array indexes go from 0 to arr.length - 1, so accessing arr[3] is undefined and may cause unexpected output.
Final Answer:
The loop condition should be i < arr.length, not i <= arr.length -> Option A
Quick Check:
Array indexes go from 0 to length-1 [OK]
Hint: Loop index must be less than length, not less or equal [OK]
Common Mistakes:
Using <= instead of < in loop condition
Starting index at 1 instead of 0
Thinking for-of is mandatory
5. Given an array arr of numbers, which code snippet correctly creates a new array containing only the even numbers from arr using array traversal?
hard
A. const evens = [];
for (let x of arr) {
if (x % 2 === 0) evens.push(x);
}
B. const evens = arr.filter(x => x % 2 === 0);
C. All of the above
D. const evens = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) evens.push(arr[i]);
}
Solution
Step 1: Analyze const evens = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) evens.push(arr[i]);
}
Uses a for loop with index to check each element and push even numbers. Correct traversal and filtering.
Step 2: Analyze const evens = arr.filter(x => x % 2 === 0); and C
const evens = arr.filter(x => x % 2 === 0); uses filter method which internally traverses array and filters evens. const evens = [];
for (let x of arr) {
if (x % 2 === 0) evens.push(x);
} uses for-of loop to traverse and filter evens. Both are correct.
Step 3: Combine understanding
All three options correctly traverse and select even numbers.
Final Answer:
All of the above -> Option C
Quick Check:
Multiple traversal patterns can filter arrays correctly [OK]
Hint: Any loop or filter method can select elements by condition [OK]
Common Mistakes:
Assuming only for loop works
Confusing filter syntax
Forgetting to check modulo condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.