7.3 — Quick Revision: Arrays
<< Overview
Array operations complexity
| Operation | Time |
|---|
| Access by index | O(1) |
| Push / pop (end) | O(1) |
| Unshift / shift (front) | O(n) |
| Insert / delete at index | O(n) |
| Linear search | O(n) |
| Binary search (sorted) | O(log n) |
Key algorithms
| Algorithm | Time | Space | Pattern |
|---|
| Kadane's (max subarray) | O(n) | O(1) | Running max |
| Two-pointer pair sum | O(n) | O(1) | Sorted array |
| Dutch National Flag | O(n) | O(1) | 3-way partition |
| Prefix sum | O(n) build, O(1) query | O(n) | Cumulative sum |
| Sliding window | O(n) | O(1) | Fixed/variable window |
| Rotation (3 reversals) | O(n) | O(1) | In-place |
Array diagram reference
Index: 0 1 2 3 4
┌───┬───┬───┬───┬───┐
Array: │ a │ b │ c │ d │ e │
└───┴───┴───┴───┴───┘
Two pointers:
L → ← R
↓ ↓
[1, 2, 3, 4, 5, 6, 7]
Sliding window (size 3):
┌─────────┐
[1, 2, 3, 4, 5, 6, 7]
┌─────────┐
[1, 2, 3, 4, 5, 6, 7]
Prefix sum:
arr: [3, 1, 4, 1, 5]
prefix: [3, 4, 8, 9, 14]
sum(1,3) = prefix[3] - prefix[0] = 9 - 3 = 6
Common pitfalls
- Off-by-one errors (< vs <=)
- Modifying array while iterating (use copy or reverse iteration)
- Integer overflow in sum calculations
- Not handling empty array
- Confusing in-place vs new-array methods
Self-check drill
SC-001
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-002
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-003
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-004
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-005
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-006
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-007
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-008
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-009
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-010
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-011
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-012
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-013
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-014
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-015
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-016
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-017
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-018
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-019
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-020
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-021
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-022
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-023
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-024
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-025
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-026
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-027
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-028
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-029
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-030
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-031
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-032
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-033
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-034
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-035
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-036
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-037
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-038
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-039
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-040
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-041
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-042
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-043
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-044
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-045
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-046
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-047
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-048
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-049
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-050
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-051
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-052
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-053
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-054
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-055
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-056
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-057
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-058
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-059
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-060
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-061
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-062
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-063
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-064
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-065
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-066
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-067
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-068
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-069
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-070
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-071
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-072
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-073
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-074
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-075
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-076
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-077
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-078
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-079
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-080
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-081
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-082
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-083
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-084
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-085
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-086
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-087
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-088
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-089
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-090
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-091
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-092
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-093
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-094
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-095
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-096
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-097
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-098
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-099
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-100
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-101
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-102
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-103
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-104
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-105
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-106
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-107
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-108
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-109
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-110
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-111
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-112
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-113
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-114
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-115
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-116
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-117
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-118
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-119
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-120
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-121
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-122
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-123
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-124
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-125
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-126
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-127
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-128
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-129
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-130
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-131
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-132
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-133
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-134
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-135
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-136
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-137
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-138
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-139
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-140
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-141
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-142
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-143
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-144
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-145
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-146
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-147
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-148
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-149
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-150
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-151
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-152
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-153
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-154
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-155
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-156
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-157
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-158
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-159
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-160
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-161
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-162
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-163
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-164
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-165
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-166
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-167
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-168
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-169
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-170
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-171
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-172
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-173
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-174
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-175
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-176
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-177
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-178
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-179
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-180
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-181
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-182
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-183
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-184
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-185
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-186
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-187
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-188
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-189
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-190
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)
SC-191
- Q: What is array access time complexity?
- A: O(1) — index-based direct calculation
SC-192
- Q: Explain Kadane's algorithm in one sentence.
- A: Track max of (extend current subarray or start new) at each element
SC-193
- Q: What is the two-pointer technique?
- A: Two indices move based on conditions, avoiding nested loops
SC-194
- Q: How to rotate array in O(n), O(1) space?
- A: Three reversals: full, first k, rest
SC-195
- Q: What is a prefix sum used for?
- A: O(1) range sum queries after O(n) preprocessing
SC-196
- Q: What does Dutch National Flag solve?
- A: Sort array of 0s, 1s, 2s in single pass with 3 pointers
SC-197
- Q: How to find missing number in 1..N?
- A: Sum formula n(n+1)/2 minus array sum, or XOR all values
SC-198
- Q: What is sliding window?
- A: Fixed-size window slides, maintaining running sum/state
SC-199
- Q: Time complexity of push vs unshift?
- A: push is O(1) amortized, unshift is O(n) due to shifting
SC-200
- Q: Boyer-Moore voting — what does it find?
- A: The majority element (appears > n/2 times)