Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start searching from the top-right corner of the matrix.
DSA Javascript
let row = 0; let col = matrix[0].length [1] 1;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - causes index out of range.
Using * or / is not correct for index calculation.
✗ Incorrect
We start from the last column index, so we subtract 1 from the length.
2fill in blank
mediumComplete the code to move left when the current element is greater than the target.
DSA Javascript
if (matrix[row][col] > target) { col [1] 1; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using += increases the column index, moving right instead of left.
Using ++ or -- alone changes by 1 but does not assign.
✗ Incorrect
We decrease the column index by 1 to move left.
3fill in blank
hardFix the error in the loop condition to prevent out-of-bound errors.
DSA Javascript
while (row < matrix.length && col [1] 0) { // search logic }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > 0 skips checking column 0.
Using < or <= causes logic errors in loop.
✗ Incorrect
Column index must be greater than or equal to 0 to stay within bounds and check column 0.
4fill in blank
hardFill both blanks to correctly move down or left based on comparison.
DSA Javascript
if (matrix[row][col] < target) { row [1] 1; } else { col [2] 1; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -= to move down or += to move left is incorrect.
Using ++ or -- without assignment causes errors.
✗ Incorrect
Increase row to move down, decrease col to move left.
5fill in blank
hardFill all three blanks to complete the search function returning true if target found, else false.
DSA Javascript
function searchMatrix(matrix, target) {
let row = 0;
let col = matrix[0].length [1] 1;
while (row < matrix.length && col [2] 0) {
if (matrix[row][col] === target) {
return [3];
} else if (matrix[row][col] < target) {
row += 1;
} else {
col -= 1;
}
}
return false;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - for last column index.
Using < or <= in loop condition causing errors.
Returning false instead of true when found.
✗ Incorrect
Start from last column (length - 1), loop while col >= 0, return true if found.