Arrays MCQs for Data Structures
1.
Q: Given array A = [2,5,7,9]. What is the index of element 7 (0-based)?
A. 1
B. 2
C. 3
D. 4
Answer: B
Solution: 0-based indexing: A[0]=2, A[1]=5, A[2]=7 → index = 2.
2.
Q: Array A=[3,3,4,5,3]. Number of occurrences of 3 is:
A. 2
B. 3
C. 4
D. 5
Answer: B
Solution: Elements equal to 3: positions 0,1,4 → total 3.
3.
Q: For array A=[1,2,3,4,5], what is sum(A[1..3]) (inclusive, 0-based)?
A. 5
B. 6
C. 9
D. 12
Answer: C
Solution: A[1]=2, A[2]=3, A[3]=4 → sum = 2+3+4 = 9.
4.
Q: Given A=[4,1,6,2]. After swapping A[0] and A[3], array becomes:
A. [2,1,6,4]
B. [4,2,6,1]
C. [2,1,4,6]
D. [1,4,6,2]
Answer: A
Solution: Swap indices 0 and 3: [2,1,6,4].
5.
Q: If an array of size n=7 has last valid index equal to:
A. 6
B. 7
C. 8
D. 5
Answer: A
Solution: 0-based indices → last index = n-1 = 6.
6.
Q: Given sorted array A=[1,4,7,10,13]. Binary search for key 10 will check middle index first (0..4). Which index is middle?
A. 1
B. 2
C. 3
D. 4
Answer: B
Solution: (0+4)//2 = 2 → middle index 2.
7.
Q: For circular array of size 6, next index after 5 is:
A. 5
B. 6
C. 0
D. 1
Answer: C
Solution: Circular wrap: (5+1)%6 = 0.
8.
Q: Array A=[2,4,6,8], average value equals:
A. 4
B. 5
C. 6
D. 7
Answer: B
Solution: Sum = 20, avg = 20/4 = 5.
9.
Q: Given A=[1,2,3,2,1]. Which index is the first peak (local maximum) considering only interior indices?
A. 0
B. 1
C. 2
D. 3
Answer: C
Solution: Interior indices 1..3; index2 value=3 > neighbors 2 and 2 → first peak at 2.
10.
Q: If array A is stored contiguously and sizeof(int)=4, address of A[3] (base address 1000) is:
A. 1012
B. 1003
C. 1008
D. 1016
Answer: A
Solution: Address = base + 3*4 = 1000+12=1012.
11.
Q: For array A=[5,1,3,2], how many inversions (pairs iA[j])?
A. 2
B. 3
C. 4
D. 5
Answer: B
Solution: Inversions: (5,1),(5,3),(5,2) = 3.
12.
Q: A=[1,2,3,4]. After performing one right rotation, array becomes:
A. [4,1,2,3]
B. [2,3,4,1]
C. [1,4,2,3]
D. [3,4,1,2]
Answer: A
Solution: Right rotate: last element moves to front.
13.
Q: Given A=[2,7,2,7,2]. The majority element (>n/2) is:
A. 2
B. 7
C. None
D. Both
Answer: A
Solution: n=5, 2 occurs 3 times >2.5 → majority is 2.
14.
Q: Number of subarrays of array of length n=5 is:
A. 10
B. 15
C. 20
D. 25
Answer: B
Solution: Number of subarrays = n(n+1)/2 = 5*6/2 = 15.
15.
Q: The space complexity to store an array of n integers is:
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Answer: C
Solution: Storing n items requires linear space O(n).
16.
Q: Array A=[1,3,5,7,9]. If we perform binary search for key 2, number of comparisons in worst case is (ceil(log2(n+1)))? For n=5 → approx:
A. 2
B. 3
C. 4
D. 5
Answer: B
Solution: Worst-case comparisons ≈ ⌈log2(5)⌉ = 3.
17.
Q: For A=[1,1,1,1,1], after removing duplicates in place, length becomes:
A. 0
B. 1
C. 5
D. 4
Answer: B
Solution: Unique element 1 only → length 1.
18.
Q: Given A=[2,4,6,8,10]. Which is true about indices increasing: A[i+1]-A[i] = constant equals:
A. 1
B. 2
C. 3
D. 4
Answer: B
Solution: Differences: 4-2=2, 6-4=2 → constant 2.
19.
Q: To reverse an array in-place of length n=6, number of swaps needed is:
A. 2
B. 3
C. 4
D. 6
Answer: B
Solution: Need floor(n/2)=3 swaps.
20.
Q: Array A=[0,1,0,1,0]. Number of contiguous segments of 1s is:
A. 1
B. 2
C. 3
D. 4
Answer: B
Solution: Segments: [1] at index1 and [1] at index3 → 2 segments.
21.
Q: In an unsorted array of n=8, worst-case linear search comparisons to find an element not present =
A. 1
B. n/2
C. n
D. n-1
Answer: C
Solution: Must check all n elements → n comparisons.
22.
Q: For array A=[3,8,9,2], prefix sums array P (P[0]=A[0]) at index 2 equals:
A. 3
B. 11
C. 20
D. 22
Answer: B
Solution: P[2]=A[0]+A[1]+A[2]=3+8+9=20 → Wait careful: index 2 → 20. So correct option among choices is C.
Correction: Answer: C
Solution: Sum = 20.
23.
Q: Given A=[1,2,3,4,5]. Which operation has O(n) time:
A. Access A[2]
B. Insert at end (if space)
C. Delete at middle shifting elements
D. Compute A[4]
Answer: C
Solution: Deleting at middle requires shifting ~O(n) elements.
24.
Q: An array of size 10 holds integers in continuous memory. If we perform memcpy of whole array to another location, time complexity is:
A. O(1)
B. O(log n)
C. O(n)
D. O(n^2)
Answer: C
Solution: Copying n elements → linear time O(n).
25.
Q: In array A=[2,3,5,7,11], median is:
A. 3
B. 5
C. 6
D. 7
Answer: B
Solution: Sorted, odd n=5 → middle element A[2]=5.
26.
Q: For array A=[6,6,6,6], after applying stable sort nothing changes. Stable sort means:
A. Relative order may change
B. Equal elements keep relative order
C. Sorting impossible
D. Algorithm uses extra space always
Answer: B
Solution: Stable sort preserves relative order of equal keys.
27.
Q: A=[1,0,1,0,1,0]. Minimum number of swaps to group all 1s together? There are three 1s. Answer:
A. 0
B. 1
C. 2
D. 3
Answer: B
Solution: Sliding window of size 3; best window has two 1s already so need 1 swap → minimal swaps = 1.
28.
Q: Array A of length n=9 contains elements 1..9 unique. The index of element 9 in zero-based sorted array is:
A. 7
B. 8
C. 1
D. 0
Answer: B
Solution: 9 is largest, index n-1 = 8.
29.
Q: Which of these is false for static arrays?
A. Fixed size at compile/run allocation
B. O(1) random access
C. Can grow automatically when full
D. Contiguous memory layout
Answer: C
Solution: Static arrays cannot auto-grow; that’s dynamic array/vector behavior.
30.
Q: Given A=[4,2,9,7,1]. After one pass of selection sort (select min and swap with A[0]), array becomes:
A. [1,2,9,7,4]
B. [2,4,9,7,1]
C. [1,4,9,7,2]
D. [4,2,9,7,1]
Answer: A
Solution: Minimum is 1 at index4 → swap with A[0] yields [1,2,9,7,4].
31.
Q: For array A=[1,2,3,4,5], the number of ways to choose two indices i<j is:
A. 5
B. 10
C. 15
D. 20
Answer: B
Solution: nC2 = 5*4/2 =10.
32.
Q: A=[2,3,2,3,2]. If we compress consecutive duplicates (run-length encode), length reduces to:
A. 3
B. 4
C. 5
D. 2
Answer: A
Solution: Runs: [2],[3],[2],[3],[2] → actually runs alternate, so runs = 5. Wait careful: consecutive runs: 2,3,2,3,2 → all are single-length runs → count =5. None of choices match? Closest is C (5).
Correction: Answer: C
Solution: There are 5 runs.
33.
Q: Given A=[1,3,5,7] and B=[2,4,6,8] merging two sorted arrays yields element at index 4 (0-based) is:
A. 5
B. 6
C. 7
D. 8
Answer: B
Solution: Merged = [1,2,3,4,5,6,7,8] → index4 = 5? Wait compute: indices 0..7 → 0:1,1:2,2:3,3:4,4:5,5:6. So index4 =5. Correct option A.
Correction: Answer: A
Solution: Element at index4 is 5.
34.
Q: For an array of n elements, building a frequency map using hashing takes average time:
A. O(n)
B. O(n log n)
C. O(n^2)
D. O(log n)
Answer: A
Solution: Each element inserted/updated in average O(1) → total O(n).
35.
Q: For A=[1,2,1,2,1], which index is median of medians approach pivot candidate roughly? (No numeric answer necessary — conceptual) The algorithm partitions into groups of:
A. 3
B. 5
C. 7
D. 2
Answer: A
Solution: Median-of-medians groups of 5 in theory, but common simplified approach uses groups of 3? However classical median-of-medians groups of 5. So correct is B.
Correction: Answer: B
Solution: Median-of-medians uses groups of 5 to guarantee linear time.
36.
Q: Array A=[10,5,2,3]. What is maximum subarray sum (Kadane)?
A. 10
B. 15
C. 20
D. 0
Answer: A
Solution: All positives? No negatives except none. Sum of all = 20. Wait sum = 10+5+2+3 = 20 → maximum subarray sum is 20. Correct option C.
Correction: Answer: C
Solution: Entire array sum = 20.
37.
Q: A=[1,2,3,4,5]. After left rotate by 2, array becomes:
A. [3,4,5,1,2]
B. [4,5,1,2,3]
C. [5,1,2,3,4]
D. [2,3,4,5,1]
Answer: A
Solution: Left rotate by 2 moves first two to end.
38.
Q: For A=[5,4,3,2,1]. Number of comparisons in bubble sort worst-case ≈ n(n-1)/2 = for n=5:
A. 5
B. 10
C. 15
D. 20
Answer: B
Solution: 5*4/2 = 10 comparisons in this formula (actually comparisons ~10).
39.
Q: Given array A of size n stored in row-major order for 2D mapped into 1D. Accessing element (i,j) cost is O(1) due to:
A. Pointer chasing
B. Contiguous memory arithmetic
C. Recursion
D. Hashing
Answer: B
Solution: Row-major layout allows direct address computation → O(1).
40.
Q: A=[1,3,2,3,1]. Longest symmetric prefix-suffix length (proper) is:
A. 0
B. 1
C. 2
D. 3
Answer: B
Solution: Proper prefix which is also suffix: [1] matches → length 1.
41.
Q: For array A=[1,2,3,4,5], sum of all pairs (i<j) A[i]*A[j] equals:
A. 35
B. 40
C. 55
D. 70
Answer: A
Solution: Sum pairs = ( (sum)^2 – sum of squares )/2 = (15^2 – 55)/2 = (225-55)/2=170/2=85. But options not match — recalc: sum squares =1+4+9+16+25=55; (225-55)/2=85. Option none. I must have mis-posed. Let’s fix numbers: For A=[1,2,3,4], sum s=10, squares=30 → (100-30)/2=35. Option A=35 fits for n=4. So correction: Question intended A=[1,2,3,4].
Correction: For A=[1,2,3,4], Answer: A (35).
Solution: Using formula gives 35.
42.
Q: A=[1,2,3,2,1]. Number of palindromic subarrays of odd length at center index 2 is:
A. 1
B. 2
C. 3
D. 4
Answer: C
Solution: Center at 2 (value 3): palindromes: [3], [2,3,2], [1,2,3,2,1] → 3.
43.
Q: For array of unique elements, selection of k-th smallest via Quickselect average-case time is:
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Answer: C
Solution: Quickselect average-case linear O(n).
44.
Q: A=[0,2,4,6,8]. XOR of all elements equals: (compute)
A. 0
B. 2
C. 6
D. 12
Answer: Let’s compute: 0^2=2; 2^4=6; 6^6=0; 0^8=8 → result 8. None of options. I must craft consistent options. Replace options: A. 8 B. 0 C. 6 D. 2. Answer: A (8).
Solution: XOR sequentially yields 8.
45.
Q: Given A=[1,2,3,4,5], using prefix-sum technique to compute sum of every k=3-length subarray — number of such sums = n-k+1 = ?
A. 1
B. 2
C. 3
D. 4
Answer: D
Solution: 5-3+1=3? Wait compute: 5-3+1=3. So correct is C.
Correction: Answer: C
Solution: There are 3 windows of size 3.
46.
Q: In a rotated sorted array [6,7,8,1,2,3,4,5], the pivot (smallest element) index is:
A. 0
B. 3
C. 4
D. 7
Answer: B
Solution: Smallest element 1 at index 3.
47.
Q: For array A=[1,2,3,4], what is complexity to find two-sum pair using hashing?
A. O(n^2)
B. O(n log n)
C. O(n) average
D. O(log n)
Answer: C
Solution: Hash set approach finds pair in average O(n) time.
48.
Q: Given A=[5,0,3,8]. Which index holds the minimum element?
A. 0
B. 1
C. 2
D. 3
Answer: B
Solution: Minimum is 0 at index1.
49.
Q: For array A=[2,2,2,2], the best-case time for bubble sort (already sorted) using optimized version is:
A. O(1)
B. O(n)
C. O(n^2)
D. O(log n)
Answer: B
Solution: With early-exit flag, single pass detects sorted → O(n).
50.
Q: A=[1,2,3,4,5,6]. Number of even-sum subarrays? (Quick reasoning: any subarray with even count of odd numbers) Instead craft simpler: Number of subarrays total = 21. If number of even-sum subarrays = ? This is long; better replace with: For array of n elements, number of contiguous subarrays with even sum can be found in O(n). Complexity answer only.
Which is the time complexity to count even-sum subarrays using prefix parity counts?
A. O(n^2)
B. O(n log n)
C. O(n)
D. O(1)
Answer: C
Solution: Use prefix parity counts in single pass → O(n).
51.
Q: Given A=[3,1,4,1,5]. Index of first occurrence of 1 is:
A. 0
B. 1
C. 2
D. 3
Answer: B
Solution: A[1]=1 is first occurrence.
52.
Q: In dynamic array doubling strategy, amortized cost per push is:
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Answer: A
Solution: Doubling gives amortized O(1) insertion.
53.
Q: For A=[1,2,3,2,1], is array symmetric around center?
A. Yes
B. No
C. Depends on rotation
D. Only after reversing
Answer: A
Solution: A[i]==A[n-1-i] for all i → symmetric (palindrome).
54.
Q: Given array indexes i and j in 0..n-1, swapping values at two indices is:
A. O(1) time, O(1) space
B. O(n) time
C. O(log n) time
D. O(1) time, O(n) space
Answer: A
Solution: Single swap uses constant operations and space.
55.
Q: Which algorithm finds smallest missing positive in unsorted array in O(n) time and O(1) extra space?
A. Sorting then scan
B. Hashing
C. Index marking (cyclic)
D. Brute force
Answer: C
Solution: Use index as presence marker (place numbers in their index) → linear time constant space.
56.
Q: A=[2,5,1,3,4]. After one pass of insertion sort (inserting A[1] into sorted prefix A[0]) array becomes:
A. [2,5,1,3,4]
B. 2,5,1,3,4
C. [2,5,1,3,4] duplicates — ambiguous. Let’s instead craft: After inserting A[2]=1 into prefix [2,5], result prefix becomes:
Options: A. [1,2,5,…] B. [2,1,5,…] C. [5,2,1,…] D. [1,5,2,…]
Answer: A
Solution: Insertion places 1 at front, prefix sorted [1,2,5].
57.
Q: For an array A of n integers, computing all prefix minima takes time:
A. O(1)
B. O(n)
C. O(n log n)
D. O(n^2)
Answer: B
Solution: One pass maintaining current minimum → O(n).
58.
Q: A=[1,2,3,4,5]. Sum of elements at even indices (0-based: 0,2,4) =
A. 9
B. 8
C. 7
D. 6
Answer: A
Solution: A[0]+A[2]+A[4]=1+3+5=9.
59.
Q: Which of these operations is NOT allowed in constant time on a singly linked list but is on an array?
A. Random access A[i]
B. Inserting at head
C. Deleting at head
D. Iteration
Answer: A
Solution: Random access O(1) for arrays, O(n) for singly linked list.
60.
Q: For array A of n distinct integers, counting inversions can be done in O(n log n) time using:
A. Naive pair counting
B. Merge sort modification
C. Bubble sort
D. Selection sort
Answer: B
Solution: Merge-sort based inversion counting achieves O(n log n).
61.
Q: A=[1,0,1,0,1]. Minimum swaps to make array alternate starting with 0? For n=5, expected swaps:
A. 0
B. 1
C. 2
D. 3
Answer: B
Solution: Compare with pattern 0,1,0,1,0 → mismatches count/2 → one swap needed.
62.
Q: In two-pointer technique on sorted array to find pair sum, time complexity is:
A. O(n^2)
B. O(n log n)
C. O(n)
D. O(log n)
Answer: C
Solution: Two pointers move inward at most n steps total → O(n).
63.
Q: Given A=[1,2,3,4,5]. After performing reverse(A[1..3]) (inclusive), array becomes:
A. [1,4,3,2,5]
B. [1,3,2,4,5]
C. [4,3,2,1,5]
D. [1,2,3,4,5]
Answer: A
Solution: Reverse segment 2,3,4 -> becomes 4,3,2.
64.
Q: For A=[1,2,3,2,1], longest increasing contiguous subarray length is:
A. 1
B. 2
C. 3
D. 4
Answer: C
Solution: Increasing segments: [1,2,3] length 3 is longest.
65.
Q: If array contains numbers from 1..n with one duplicate and one missing, you can find both using:
A. Sorting only
B. XOR and sum formulas in O(n) time
C. Brute force O(n^2)
D. Insertion sort
Answer: B
Solution: Using XOR and difference between expected and actual sum and sum of squares yields O(n).
66.
Q: For array A=[8,1,2,8,3,8], which element is the mode (most frequent)?
A. 1
B. 2
C. 3
D. 8
Answer: D
Solution: 8 appears three times, others less.
67.
Q: A=[4,1,3,2]. Minimum number of adjacent swaps to sort (bubble swap count) equals inversion count = ? Inversions: (4,1),(4,3),(4,2),(3,2) = 4. Options: A.2 B.3 C.4 D.5
Answer: C
Solution: Inversion count = 4, equals minimum adjacent swaps.
68.
Q: For array of n elements, copying a subarray of length k is O(k). If k = n/2, cost is:
A. O(1)
B. O(n)
C. O(n log n)
D. O(n^2)
Answer: B
Solution: k proportional to n → O(n).
69.
Q: A=[1,2,3,4,5]. If we store cumulative sum in same array (in-place prefix), after processing index i we store sum up to i. After operation final A is:
A. [1,3,6,10,15]
B. [1,2,3,4,5]
C. [15,14,12,9,5]
D. [5,9,12,14,15]
Answer: A
Solution: In-place prefix sums produce running totals.
70.
Q: In array A=[2,3,4,3,2], remove duplicates but keep first occurrence; resulting array begins with:
A. [2,3,4,…]
B. [3,4,2,…]
C. [4,3,2,…]
D. [2,4,3,…]
Answer: A
Solution: Keeping first occurrences preserves order → 2,3,4.
71.
Q: Which operation requires shifting O(n) elements in an array?
A. Append at end when capacity available
B. Pop from end
C. Insert at index 0
D. Access element at index i
Answer: C
Solution: Insert at beginning requires shifting all elements → O(n).
72.
Q: A=[1,5,2,4,3]. After one partition step of quicksort with pivot=3 (Lomuto), resulting partition (elements < pivot left) might be:
A. [1,2,3,5,4]
B. [5,2,4,1,3]
C. [3,1,2,4,5]
D. [1,5,2,4,3]
Answer: A
Solution: After partition, pivot 3 ends at correct place with smaller left: possible [1,2,3,5,4].
73.
Q: For array A with length n, converting it into a heap (build-heap) takes time:
A. O(n log n)
B. O(n)
C. O(log n)
D. O(1)
Answer: B
Solution: Build-heap bottom-up runs in linear time O(n).
74.
Q: A=[2,4,6,8,10]. Using binary search to find 7 returns negative (not found). Number of comparisons executed approx:
A. 1
B. 2
C. 3
D. 4
Answer: C
Solution: log2(5) ≈ 3 comparisons in worst case.
75.
Q: For array A, rotating right by k where k can be larger than n, correct effective k equals:
A. k mod n
B. k + n
C. n – k
D. floor(k/n)
Answer: A
Solution: Rotating by multiples of n returns same array; effective shift = k % n.
76.
Q: A=[1,2,3,4,5]. After applying map function f(x)=2x to all elements, result is:
A. [2,4,6,8,10]
B. [1,2,3,4,5]
C. [0,2,4,6,8]
D. [1,4,9,16,25]
Answer: A
Solution: Multiply each element by 2.
77.
Q: In array A=[1,2,3,1,2,3], shortest subarray covering all unique elements {1,2,3} has length:
A. 2
B. 3
C. 4
D. 6
Answer: B
Solution: Window [1,2,3] length 3 covers all.
78.
Q: For array A with n elements, typical cost to access A[i] is:
A. O(n)
B. O(log n)
C. O(1)
D. O(n log n)
Answer: C
Solution: Direct indexing into contiguous array is O(1).
79.
Q: A=[0,0,1,1,0]. Using stable partition to move all zeros before ones results in:
A. [0,0,0,1,1]
B. [1,1,0,0,0]
C. [0,1,0,1,0]
D. [0,0,1,1,0]
Answer: A
Solution: Stable partition preserves relative order within groups; final zeros then ones.
80.
Q: For A=[3,1,2,4], number of cyclic shifts that make array sorted ascending is:
A. 0
B. 1
C. 2
D. 3
Answer: A
Solution: No rotation of the array yields sorted ascending [1,2,3,4]. So 0.
81.
Q: Given A=[2,4,6,8,10], index of element 8 is:
A. 2
B. 3
C. 4
D. 1
Answer: B
Solution: 0-based: A[0]=2,A[1]=4,A[2]=6,A[3]=8 → index 3.
82.
Q: A=[1,2,3,4]. Copying array to another array of equal size via loop is O(n). Number of assignments for n=4 is:
A. 2
B. 3
C. 4
D. 5
Answer: C
Solution: Each of 4 elements assigned once → 4 assignments.
83.
Q: For array A=[1,3,2,4], number of prefix arrays that are permutations of 1..k for some k? (prefix k=1 -> [1] ok; k=2 -> [1,3] not; k=3 -> [1,3,2] yes (permutation of 1..3); k=4 -> yes). Count =? Options: A.1 B.2 C.3 D.4
Answer: C
Solution: Valid prefixes: k=1,k=3,k=4 → total 3.
84.
Q: For A=[1,2,3,4,5], computing sliding window maximum of size k=3 using deque approach time complexity:
A. O(n^2)
B. O(n log n)
C. O(n)
D. O(k)
Answer: C
Solution: Deque algorithm processes each element once → O(n).
85.
Q: A=[1,100,2,99,3]. Peak elements (greater than neighbors) count is: elements 100 and 99 are peaks. Total peaks =
A. 1
B. 2
C. 3
D. 4
Answer: B
Solution: 100 at index1 greater than 1 and 2; 99 at index3 greater than 2 and 3 → 2 peaks.
86.
Q: In an array, stable in-place removal of element at index i while preserving order requires shifting O(n). So time is:
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Answer: C
Solution: Must shift subsequent elements left by one → linear.
87.
Q: Given A=[0,1,2,3,4,5]. After setting A[i] = A[i] + A[i-1] for i from 1..5 sequentially, final A[5] equals:
A. 15
B. 31
C. 20
D. 21
Answer: B
Solution: This produces doubling-like sums: computation yields final A = [0,1,3,6,10,15]? Wait calculate: start A0=0; i=1: A1=1+0=1; i2: A2=2+1=3; i3:3+3=6; i4:4+6=10; i5:5+10=15 → A5=15. So correct A.
Answer: A (15).
Solution: Final A[5]=15.
88.
Q: For array A sorted ascending, rank query (number of elements ≤ x) can be answered by binary search in time:
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Answer: B
Solution: Binary search finds upper_bound in O(log n).
89.
Q: A=[1,2,3,4,5]. Number of contiguous subarrays with odd length = ? For n=5, count equals sum over i (number of odd-length subarrays including element i)/? Simpler known formula: number of odd-length subarrays = ceil(n/2)ceil(n/2) + floor(n/2)floor(n/2)?? Let’s avoid complex. Replace Q: For n=5, total subarrays = 15 → odd-length subarrays count = 9. Options: A.7 B.8 C.9 D.10
Answer: C
Solution: For n=5, number of odd-length subarrays = (n+1)/2 * (n+1)/2 + … but value 9 correct by enumeration.
90.
Q: Given A=[1,2,2,3,3,3]. Distinct element count is:
A. 2
B. 3
C. 4
D. 6
Answer: B
Solution: Distinct elements are {1,2,3} → 3.
91.
Q: A=[7,7,7]. After using binary search for first occurrence of 7, leftmost index returned is:
A. 0
B. 1
C. 2
D. -1
Answer: A
Solution: First occurrence at index 0.
92.
Q: Which data structure provides faster insertion at arbitrary index than array?
A. Dynamic array
B. Linked list (singly, given pointer to position)
C. Hash table
D. Stack
Answer: B
Solution: Given direct pointer to node, insertion O(1) in linked list; arrays require shifting.
93.
Q: If you need to maintain order and allow O(log n) search and O(log n) insert, which is better than array?
A. Balanced BST
B. Static array
C. Queue
D. Stack
Answer: A
Solution: Balanced BST gives O(log n) search/insert while preserving sorted order.
94.
Q: In an array of characters, to check if it is an anagram of another you can count frequency in O(n). So complexity:
A. O(n^2)
B. O(n log n)
C. O(n)
D. O(1)
Answer: C
Solution: Use frequency arrays or hash map → linear time.
95.
Q: A=[1,4,2,5,3]. The element at index floor(n/2) for n=5 is index2 value=2. Which option?
A. 1
B. 2
C. 3
D. 4
Answer: B
Solution: floor(5/2)=2 → A[2]=2.
96.
Q: For an array of size n, the number of distinct pairs (i,j) with i≠j equals:
A. n
B. n(n-1)/2
C. n(n-1)
D. n^2
Answer: C
Solution: Ordered pairs i≠j count = n*(n-1).
97.
Q: A=[1,2,1,2,1]. Using Boyer-Moore majority vote finds candidate 1. After verifying, it’s majority? For n=5, occurrences of 1 = 3 > 2.5 → True. So majority exists. Which step ensures final candidate is actual majority?
A. Single pass only
B. Second pass to verify count
C. Sorting
D. Randomized check
Answer: B
Solution: Boyer-Moore requires verification pass to confirm candidate count > n/2.
98.
Q: Given array A=[2,3,4,5,6,7]. Using two pointers to find pair sum 9, pointers start at 0 and 5: steps required until found? 0-based: 2+7=9 → found immediately → steps 1. Options: A.1 B.2 C.3 D.4
Answer: A
Solution: Left=0,right=5 → sum=9 found in one comparison.
99.
Q: For an array A used as stack, push and pop are O(1) amortized when using dynamic array with doubling. Which operation is expensive occasionally?
A. Push when capacity full (reallocation + copy)
B. Pop when empty
C. Top operation
D. Peek second element
Answer: A
Solution: Push that triggers resize causes O(n) but amortized over many pushes remains O(1).
100.
Q: A=[-1,2,-3,4,-5]. Maximum subarray sum (Kadane) equals:
A. 4
B. 1
C. 2
D. 0
Answer: A
Solution: Best subarray is [4] giving sum 4 (other positive combinations smaller).