Episode 7 — DSA with JavaScript / 7.8 — Advanced Array Problems

7.8 — Interview Questions: Advanced Array Problems

<< Overview


Q1. Container with most water — explain approach.

Answer: Two pointers from ends. Move the shorter wall inward. O(n).

Q2. Three Sum — how to avoid duplicates?

Answer: Sort array, skip duplicate values for i, l, r.

Q3. Explain prefix sum for subarray sum = K.

Answer: Store cumulative sums in a map. Check if (sum-k) exists. O(n).

Q4. Spiral matrix traversal algorithm?

Answer: Use 4 boundaries (top/bottom/left/right), traverse and shrink.

Q5. How to rotate a matrix 90° in-place?

Answer: Transpose then reverse each row. O(n²) time, O(1) space.

Q6. Search in sorted 2D matrix — optimal approach?

Answer: Treat as 1D sorted array, binary search. O(log(m×n)).

Q7. Set matrix zeroes in O(1) space?

Answer: Use first row and column as markers.

Q8. Minimum window substring approach?

Answer: Variable sliding window with frequency maps. O(n).

Q9. How does the sliding window maximum work?

Answer: Use a deque maintaining indices of potential maximums. O(n).

Q10. Four Sum approach?

Answer: Sort + two nested loops + two pointers. O(n³).

Q11. Next permutation algorithm?

Answer: Find rightmost ascending pair, swap with ceiling, reverse suffix.

Q12. Longest increasing subsequence complexity?

Answer: O(n log n) with patience sorting / binary search approach.

Q13. How to count subarrays with exactly K elements?

Answer: atMost(K) - atMost(K-1) technique.

Q14. Merge intervals algorithm?

Answer: Sort by start, merge overlapping. O(n log n).


Quick-fire table

#QuestionAnswer
1Container water?Two pointers, O(n)
23Sum time?O(n²) with sort
3Prefix sum query?O(1) after O(n) build
4Spiral traverse?4 boundaries
5Rotate matrix?Transpose + reverse rows
6Search sorted 2D?Binary search O(log mn)
7Set zeroes space?O(1) using first row/col
8Min window?Variable sliding window
9Next permutation?Find, swap, reverse
10Merge intervals?Sort by start, merge

Rapid self-check cards

SC-001

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-002

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-003

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-004

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-005

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-006

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-007

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-008

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-009

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-010

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-011

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-012

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-013

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-014

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-015

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-016

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-017

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-018

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-019

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-020

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-021

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-022

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-023

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-024

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-025

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-026

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-027

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-028

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-029

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-030

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-031

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-032

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-033

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-034

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-035

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-036

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-037

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-038

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-039

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-040

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-041

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-042

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-043

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-044

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-045

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-046

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-047

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-048

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-049

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-050

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-051

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-052

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-053

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-054

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-055

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-056

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-057

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-058

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-059

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-060

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-061

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-062

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-063

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-064

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-065

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-066

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-067

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-068

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-069

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-070

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-071

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-072

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-073

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-074

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-075

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-076

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-077

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-078

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-079

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-080

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-081

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-082

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-083

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-084

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-085

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-086

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-087

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-088

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-089

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-090

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-091

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-092

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-093

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-094

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-095

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-096

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-097

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-098

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-099

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-100

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-101

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-102

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-103

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-104

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-105

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-106

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-107

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-108

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-109

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-110

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-111

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-112

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-113

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-114

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-115

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-116

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-117

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-118

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-119

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-120

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-121

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-122

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-123

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-124

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-125

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-126

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-127

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-128

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-129

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-130

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-131

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-132

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-133

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-134

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-135

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-136

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-137

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-138

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-139

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-140

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-141

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-142

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-143

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-144

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-145

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-146

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-147

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-148

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-149

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-150

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-151

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-152

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-153

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-154

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-155

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-156

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-157

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-158

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-159

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-160

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-161

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-162

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-163

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-164

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-165

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-166

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-167

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-168

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-169

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-170

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-171

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-172

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-173

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-174

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-175

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-176

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-177

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-178

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-179

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-180

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-181

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-182

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-183

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-184

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-185

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-186

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-187

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-188

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-189

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-190

  • Q: Merge intervals?
  • A: Sort by start, extend or add

SC-191

  • Q: Container with most water approach?
  • A: Two pointers from ends, move shorter

SC-192

  • Q: 3Sum complexity?
  • A: O(n²) with sorting

SC-193

  • Q: Prefix sum for subarray sum K?
  • A: Map of cumulative sums, check sum-k

SC-194

  • Q: Spiral order algorithm?
  • A: 4 boundaries: top/bottom/left/right

SC-195

  • Q: Rotate matrix 90° in-place?
  • A: Transpose then reverse each row

SC-196

  • Q: Search sorted matrix?
  • A: Binary search treating as 1D array

SC-197

  • Q: Set zeroes in O(1) space?
  • A: Use first row/column as markers

SC-198

  • Q: Sliding window maximum?
  • A: Deque of indices, O(n)

SC-199

  • Q: Next permutation?
  • A: Find rightmost ascent, swap, reverse

SC-200

  • Q: Merge intervals?
  • A: Sort by start, extend or add