Given an array of integers, how can you efficiently count the occurrences of a specific element?
Iterate through the array and increment a counter for each occurrence.
Use a hash map to store the frequency of each element.
All of the above methods are equally efficient.
Sort the array and use binary search.
How can you efficiently delete a specific element from the middle of a sorted array while maintaining the sorted order?
By directly removing the element and leaving the space empty.
By shifting all the elements after the deleted element one position back.
By swapping the element with the last element and then removing it.
Deletion in a sorted array always disrupts the order.
Which code snippet correctly initializes a 2D array named 'grid' with 3 rows and 4 columns, all filled with zeros?
int grid[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) grid[i][j] = 0;
int grid[3][4] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int grid[3][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
int grid[3][4] = {0};
Imagine a 2D array representing a grayscale image. Each element holds a pixel intensity value. How would you swap two rows 'r1' and 'r2' in this array?
Swapping rows is not possible in a 2D array.
Create a new 2D array with the swapped rows.
Iterate through columns, swapping elements at 'image[r1][j]' and 'image[r2][j]' for each column 'j'.
Directly assign 'image[r1]' to 'image[r2]' and vice-versa.
In an array with indices starting at 0, what is the index of the last element in an array of size 'n'?
n - 1
0
n
It depends on the data type of the array.
What is the space complexity of storing a 2D array with 'm' rows and 'n' columns?
O(m)
O(n)
O(m * n)
O(1)
What is the time complexity of finding the maximum element in a sorted array?
O(log n)
O(n log n)
You want to find the first occurrence of a specific element in a sorted array. Which search algorithm is the most efficient?
It depends on the size of the array.
Linear Search
Binary Search
Both are equally efficient in this case.
What is a key difference between a 1D array and a 2D array?
1D arrays store numbers while 2D arrays store characters.
1D arrays can only be accessed sequentially, 2D arrays allow random access.
1D arrays represent linear sequences, 2D arrays represent tabular data.
1D arrays are static in size, 2D arrays can change size dynamically.
What is the primary advantage of using binary search over linear search?
Binary search is simpler to implement.
Binary search uses less memory.
Binary search works on unsorted arrays.
Binary search is generally faster for large arrays.