Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the variable that will store the spiral order elements.
DSA Javascript
let result = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using an object {} instead of an array.
Initializing with null or 0 which cannot hold multiple elements.
✗ Incorrect
We use an empty array [] to store the elements in spiral order.
2fill in blank
mediumComplete the code to set the initial value of the variable that tracks the left boundary of the matrix.
DSA Javascript
let left = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Starting left at 1 or -1 which skips the first column.
Using matrix.length which is the number of rows, not columns.
✗ Incorrect
The left boundary starts at index 0, the first column of the matrix.
3fill in blank
hardFix the error in the loop condition to correctly traverse the top row from left to right.
DSA Javascript
for (let i = left; i [1]= right; i++) { result.push(matrix[top][i]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which misses the last element.
Using '>' or '>=' which causes the loop not to run or run incorrectly.
✗ Incorrect
We use '<=' to include the right boundary index in the traversal.
4fill in blank
hardFill both blanks to correctly traverse the right column from top+1 to bottom.
DSA Javascript
for (let i = [1]; i [2] bottom; i++) { result.push(matrix[i][right]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at top which repeats the corner.
Using '<' which misses the bottom element.
✗ Incorrect
Start from top + 1 to avoid repeating the corner element and use '<=' to include bottom.
5fill in blank
hardFill all three blanks to correctly traverse the bottom row from right-1 to left in reverse order.
DSA Javascript
for (let i = [1]; i [2] [3]; i--) { result.push(matrix[bottom][i]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at right which repeats the corner.
Using '<=' which is wrong for decreasing loop.
✗ Incorrect
Start from right - 1 to avoid repeating the corner, go while i >= left to include left boundary.
