programming
beginner
10 sample questions
Arrays MCQ Practice Test
Insertion, deletion, searching, and sorting algorithms.
Q1. Given a 2D array representing a matrix where each inner array represents a row, what will be the output of the following Python code snippet?
python
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
result = []
for i in range(len(matrix)):
result.append(matrix[i][len(matrix) - 1 - i])
print(result)
-
A. [3, 5, 7] ✓
-
B. [1, 5, 9]
-
C. [3, 5, 7] ✓
-
D. [7, 5, 3]
Explanation: The code iterates through the matrix using the index 'i'. In each iteration, it appends the element at matrix[i][len(matrix) - 1 - i] to the 'result' list. This effectively selects elements along the anti-diagonal of the matrix. Therefore the output will be [3, 5, 7].
Q2. Given a 2D array representing a matrix where each element is a non-negative integer, what is the most efficient approach to find the largest sum of elements within a contiguous 3x3 submatrix? Assume the matrix dimensions are significantly larger than 3x3.
-
A. Iterate through every possible 3x3 submatrix, calculating the sum for each and tracking the maximum.
-
B. Employ a dynamic programming approach to recursively calculate sums of overlapping submatrices.
-
C. Utilize a sliding window technique to efficiently compute sums of 3x3 submatrices as the window moves across the matrix. ✓
-
D. Use a divide and conquer strategy, recursively breaking down the matrix into smaller submatrices and merging the results.
Explanation: A sliding window approach avoids redundant calculations. For each 3x3 window, the sum can be efficiently updated by subtracting the element leaving the window and adding the element entering the window. This reduces the time complexity significantly compared to iterating through all submatrices individually.
Q3. Given a 2D array representing a matrix where each inner array has the same length, what will be the most efficient algorithm's time complexity to find the saddle point (an element that is the minimum in its row and maximum in its column) in the worst-case scenario? Assume the matrix dimensions are N x M.
-
A. O(N*M) ✓
-
B. O(N + M)
-
C. O(N*logM)
-
D. O(M*logN)
Explanation: In the worst-case scenario, you might need to check every element in the matrix to determine if it's a saddle point. Therefore, the time complexity is directly proportional to the number of elements, resulting in O(N*M).
Q4. Consider a 2D array representing a chessboard (8x8). You want to efficiently determine if a knight, starting at position (row1, col1), can reach position (row2, col2) in exactly three moves, considering only valid knight moves (two squares in one direction, then one square in a perpendicular direction). Which approach would be MOST efficient for solving this problem, assuming you need to solve for multiple knight positions?
-
A. A recursive depth-first search algorithm.
-
B. A breadth-first search algorithm using a queue. ✓
-
C. A dynamic programming approach to memoize possible knight positions.
-
D. A simple iterative approach checking all possible three-move combinations.
Explanation: A breadth-first search (BFS) is the most efficient for finding the shortest path (in this case, the minimum number of moves). While recursion could work, it might be less efficient due to the possibility of redundant calculations. Dynamic programming is overkill for a problem of this size; the state space is relatively small. A brute-force iterative approach would be significantly slower than BFS.
Q5. Given a 2D array representing a matrix where each inner array has a length of 5 and the outer array has a length of 3. You want to efficiently access the element at row 1, column 3 (remembering that indexing starts from zero). Which code snippet correctly accesses this element using only array indexing and assuming the array is named 'matrix'?
-
A. matrix[1][2] ✓
-
B. matrix[1][3]
-
C. matrix[0][3]
-
D. matrix[2][1]
Explanation: In a 2D array, the first index represents the row and the second index represents the column. Since we want row 1 (the second row, index 1) and column 3 (the fourth column, index 2), the correct access is matrix[1][2].
Q6. You have a 2D array representing a game board: `board = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]`. You want to efficiently count the number of '1's present in the array, but only considering elements where the sum of their row and column index is even. What will be the final count?
Explanation: Let's analyze the board:
- (0,0): 1 + 0 = 1 (odd), ignore
- (0,2): 0 + 2 = 2 (even), count 1
- (1,1): 1 + 1 = 2 (even), count 1
- (2,0): 2 + 0 = 2 (even), count 1
- (2,2): 2 + 2 = 4 (even), count 1
Total count of '1's where row + column index is even is 3.
Q7. Consider a 2D array named 'matrix' with dimensions 5x5, where each element is an integer. A function modifies 'matrix' by replacing each element with the sum of its horizontal and vertical neighbors. Boundary elements only sum with available neighbors. For example, matrix[0][0] would be replaced with matrix[0][1] + matrix[1][0]. After this transformation, what will be the value of matrix[2][2] if the initial matrix was populated with consecutive integers starting from 1 (row-major order)?
-
A. 24
-
B. 33
-
C. 52 ✓
-
D. 77
Explanation: The initial matrix will be populated as follows:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
After the transformation, matrix[2][2] (initially 13) will be the sum of its neighbors: 8 + 12 + 14 + 18 = 52
Q8. Consider a 2D array representing a game board with dimensions 5x5. You need to efficiently determine if a diagonal from the top-left to the bottom-right contains only even numbers. Assuming the array is named 'board' and indexed from [0][0] to [4][4], which code snippet correctly checks this condition?
-
A. for (int i = 0; i < 5; i++) { if (board[i][i] % 2 != 0) return false; } return true; ✓
-
B. for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (i == j && board[i][j] % 2 != 0) return false; } } return true;
-
C. int i = 0; while (i < 5 && board[i][i] % 2 == 0) i++; return i == 5;
-
D. for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++){ if (i == j && board[i][j] % 2 != 0) { return false; } else { return true;} } }
Explanation: The most efficient solution directly iterates along the main diagonal (where i == j) and checks the parity of each element. The other options either iterate unnecessarily over the entire array or have logical flaws in their conditionals.
Q9. Given a 2D array representing a matrix where each inner array has a consistent length, and a target sum, what is the most efficient approach to find if any row's elements sum to the target? Assume that elements are integers and target is also an integer.
-
A. Iterate through each row and use a separate loop to sum the elements of each row, comparing to the target.
-
B. Employ a divide-and-conquer strategy, recursively splitting the array into smaller sub-arrays.
-
C. Use dynamic programming to store and reuse intermediate sums for optimization.
-
D. Utilize a single loop, iterating through each row, calculating the sum in place, and comparing with the target. ✓
Explanation: A single loop is the most efficient as it avoids redundant calculations. Divide and conquer is unnecessarily complex for this specific problem. Dynamic programming is overkill given that the problem doesn't exhibit overlapping subproblems. A nested loop approach is less efficient than a single loop with in-place sum calculation.
Q10. You have a 2D array representing a game board, where each inner array represents a row. You need to efficiently find the coordinates (row, column) of the first occurrence of the value 'X' within the board. The board is guaranteed to contain at least one 'X'. Which approach offers the best time complexity for this search, assuming the board is not necessarily square?
-
A. Iterating through each element sequentially. ✓
-
B. Employing a binary search on each row.
-
C. Using a depth-first search algorithm.
-
D. Utilizing a hash table to store element coordinates.
Explanation: While a hash table *could* work, building it would take longer than simply iterating. Binary search requires a sorted array, which our board isn't. Depth-first search is overkill for this simple task. Sequential iteration offers O(m*n) time complexity, where 'm' is the number of rows and 'n' is the number of columns. This is the most efficient approach for this specific problem.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free