Episode 7 — DSA with JavaScript / 7.3 — Arrays

7.3 — Interview Questions: Arrays

<< Overview


Beginner

Q1. What is the time complexity of accessing an array element by index?

Answer: O(1) — constant time. Arrays store elements contiguously in memory, so the address of element i is computed as base + i × element_size.


Q2. How do you find the maximum element in an unsorted array?

Answer: Linear scan in O(n) time:

function findMax(arr) {
    let max = arr[0];
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] > max) max = arr[i];
    }
    return max;
}
int findMax(const vector<int>& arr) {
    return *max_element(arr.begin(), arr.end());
}

Q3. How do you reverse an array in-place?

Answer: Use two pointers from both ends, swap and move inward.

function reverse(arr) {
    let l = 0, r = arr.length - 1;
    while (l < r) {
        [arr[l], arr[r]] = [arr[r], arr[l]];
        l++; r--;
    }
}

Time: O(n) | Space: O(1)


Q4. What is the difference between push/pop and shift/unshift in JavaScript?

Answer:

MethodPositionTime
pushEndO(1)
popEndO(1)
unshiftBeginningO(n) — shifts all elements
shiftBeginningO(n) — shifts all elements

Q5. How do you check if an array contains a specific value?

Answer:

// JavaScript
arr.includes(value);       // O(n)
arr.indexOf(value) !== -1; // O(n)

// For sorted arrays — binary search: O(log n)
// C++
find(arr.begin(), arr.end(), value) != arr.end(); // O(n)
binary_search(arr.begin(), arr.end(), value);      // O(log n) — sorted only

Intermediate

Q6. Explain Kadane's algorithm for maximum subarray sum.

Answer: Track the current subarray sum. At each element, decide whether to extend the current subarray or start fresh.

function kadane(arr) {
    let curr = arr[0], best = arr[0];
    for (let i = 1; i < arr.length; i++) {
        curr = Math.max(arr[i], curr + arr[i]);
        best = Math.max(best, curr);
    }
    return best;
}

Key insight: If curr + arr[i] < arr[i], the previous subarray is a burden — start fresh from arr[i].

Time: O(n) | Space: O(1)


Q7. How do you rotate an array by k positions in O(n) time and O(1) space?

Answer: Three-reversal method:

  1. Reverse entire array
  2. Reverse first k elements
  3. Reverse remaining elements
function rotate(arr, k) {
    k %= arr.length;
    const rev = (l, r) => {
        while (l < r) { [arr[l], arr[r]] = [arr[r], arr[l]]; l++; r--; }
    };
    rev(0, arr.length - 1);
    rev(0, k - 1);
    rev(k, arr.length - 1);
}

Q8. Explain the two-pointer technique. Give an example.

Answer: Two pointers move through the array based on conditions, avoiding nested loops.

Example: Pair with target sum in sorted array

function twoSum(arr, target) {
    let l = 0, r = arr.length - 1;
    while (l < r) {
        const s = arr[l] + arr[r];
        if (s === target) return [l, r];
        s < target ? l++ : r--;
    }
    return [-1, -1];
}

Why it works: Sorted order guarantees that moving l right increases sum, moving r left decreases sum.


Q9. What is a prefix sum array? When is it useful?

Answer: A prefix sum array stores cumulative sums so that any range sum can be computed in O(1).

arr:    [3, 1, 4, 1, 5]
prefix: [3, 4, 8, 9, 14]

Sum(1..3) = prefix[3] - prefix[0] = 9 - 3 = 6
Check: 1 + 4 + 1 = 6 ✓

Useful for: Frequent range sum queries, subarray sum problems.


Q10. How do you find the missing number in an array of 1 to N?

Answer: Use the mathematical formula: expected sum minus actual sum.

function missingNumber(arr, n) {
    const expectedSum = n * (n + 1) / 2;
    const actualSum = arr.reduce((a, b) => a + b, 0);
    return expectedSum - actualSum;
}

Alternative: XOR all numbers 1..n with all array elements — the result is the missing number.


Advanced

Q11. Explain the Dutch National Flag algorithm.

Answer: Sort an array containing only 0, 1, 2 in a single pass using three pointers: low, mid, high.

function dnf(arr) {
    let low = 0, mid = 0, high = arr.length - 1;
    while (mid <= high) {
        if (arr[mid] === 0) { [arr[low], arr[mid]] = [arr[mid], arr[low]]; low++; mid++; }
        else if (arr[mid] === 1) { mid++; }
        else { [arr[mid], arr[high]] = [arr[high], arr[mid]]; high--; }
    }
}

Time: O(n) | Space: O(1)


Q12. Solve the "trapping rain water" problem.

Answer: Water at index i = min(maxLeft[i], maxRight[i]) - height[i].

height:   [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
maxLeft:  [0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]
maxRight: [3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1]
water:    [0, 0, 1, 0, 1, 2, 1, 0, 0, 1, 0, 0] = 6
function trap(height) {
    let left = 0, right = height.length - 1;
    let leftMax = 0, rightMax = 0, water = 0;
    while (left < right) {
        if (height[left] < height[right]) {
            leftMax = Math.max(leftMax, height[left]);
            water += leftMax - height[left];
            left++;
        } else {
            rightMax = Math.max(rightMax, height[right]);
            water += rightMax - height[right];
            right--;
        }
    }
    return water;
}

Time: O(n) | Space: O(1) with two-pointer approach


Q13. Find the majority element (appears > n/2 times).

Answer: Boyer-Moore Voting Algorithm:

function majorityElement(arr) {
    let candidate = arr[0], count = 1;
    for (let i = 1; i < arr.length; i++) {
        if (count === 0) { candidate = arr[i]; count = 1; }
        else if (arr[i] === candidate) count++;
        else count--;
    }
    return candidate;
}

Why it works: The majority element survives all cancellations because it appears more than n/2 times.

Time: O(n) | Space: O(1)


Q14. Solve "product of array except self" without division.

Answer:

function productExceptSelf(nums) {
    const n = nums.length;
    const result = new Array(n).fill(1);

    let leftProduct = 1;
    for (let i = 0; i < n; i++) {
        result[i] = leftProduct;
        leftProduct *= nums[i];
    }

    let rightProduct = 1;
    for (let i = n - 1; i >= 0; i--) {
        result[i] *= rightProduct;
        rightProduct *= nums[i];
    }

    return result;
}

Time: O(n) | Space: O(1) excluding output


Quick-fire table

#QuestionAnswer
1Array access time?O(1)
2push/pop time?O(1)
3shift/unshift time?O(n)
4Linear search time?O(n)
5Binary search time?O(log n) — sorted only
6Kadane's time?O(n)
7Two-pointer technique?Move pointers based on condition
8Prefix sum query time?O(1) after O(n) build
9Dutch National Flag?3-pointer, single pass, O(n)
10Majority element algo?Boyer-Moore Voting

Rapid self-check cards

RC-001

  • Q: What is an array?
  • A: A contiguous collection of elements accessed by index

RC-002

  • Q: Time complexity of array access by index?
  • A: O(1) — constant time via address calculation

RC-003

  • Q: How to reverse an array in-place?
  • A: Two pointers from ends, swap and move inward — O(n)

RC-004

  • Q: What is Kadane's algorithm?
  • A: Track current and global max subarray sum — O(n)

RC-005

  • Q: What is the two-pointer technique?
  • A: Two indices move based on conditions to avoid nested loops

RC-006

  • Q: What is a prefix sum?
  • A: Cumulative sum array for O(1) range queries

RC-007

  • Q: How to rotate an array by k positions?
  • A: Three reversals: full, first k, rest — O(n), O(1) space

RC-008

  • Q: What is the Dutch National Flag algorithm?
  • A: 3-pointer partition of 0s, 1s, 2s in single pass — O(n)

RC-009

  • Q: How to find the missing number in 1..N?
  • A: Expected sum n(n+1)/2 minus actual sum, or XOR method

RC-010

  • Q: What is the sliding window technique?
  • A: Fixed/variable window slides across array — O(n)

RC-011

  • Q: How to find the majority element?
  • A: Boyer-Moore: count cancellation — O(n), O(1) space

RC-012

  • Q: What is the time complexity of merge sort?
  • A: O(n log n) — divide-and-conquer

RC-013

  • Q: How to find duplicates in an array?
  • A: Sort and check adjacent O(n log n), or hash set O(n)

RC-014

  • Q: What is the difference between array and linked list?
  • A: Array: contiguous, O(1) access. Linked list: nodes, O(1) insert/delete

RC-015

  • Q: How does binary search work?
  • A: Halve search space each step — O(log n), requires sorted array

RC-016

  • Q: What is a subarray vs subsequence?
  • A: Subarray: contiguous. Subsequence: not necessarily contiguous

RC-017

  • Q: How to find the second largest element?
  • A: Single pass: track max and second max

RC-018

  • Q: What is an in-place algorithm?
  • A: Uses O(1) extra space — modifies input

RC-019

  • Q: How to merge two sorted arrays?
  • A: Two pointers, compare and place smaller — O(n+m)

RC-020

  • Q: What is a circular array?
  • A: Last element wraps to first — use modulo for indexing

RC-021

  • Q: What is an array?
  • A: A contiguous collection of elements accessed by index

RC-022

  • Q: Time complexity of array access by index?
  • A: O(1) — constant time via address calculation

RC-023

  • Q: How to reverse an array in-place?
  • A: Two pointers from ends, swap and move inward — O(n)

RC-024

  • Q: What is Kadane's algorithm?
  • A: Track current and global max subarray sum — O(n)

RC-025

  • Q: What is the two-pointer technique?
  • A: Two indices move based on conditions to avoid nested loops

RC-026

  • Q: What is a prefix sum?
  • A: Cumulative sum array for O(1) range queries

RC-027

  • Q: How to rotate an array by k positions?
  • A: Three reversals: full, first k, rest — O(n), O(1) space

RC-028

  • Q: What is the Dutch National Flag algorithm?
  • A: 3-pointer partition of 0s, 1s, 2s in single pass — O(n)

RC-029

  • Q: How to find the missing number in 1..N?
  • A: Expected sum n(n+1)/2 minus actual sum, or XOR method

RC-030

  • Q: What is the sliding window technique?
  • A: Fixed/variable window slides across array — O(n)

RC-031

  • Q: How to find the majority element?
  • A: Boyer-Moore: count cancellation — O(n), O(1) space

RC-032

  • Q: What is the time complexity of merge sort?
  • A: O(n log n) — divide-and-conquer

RC-033

  • Q: How to find duplicates in an array?
  • A: Sort and check adjacent O(n log n), or hash set O(n)

RC-034

  • Q: What is the difference between array and linked list?
  • A: Array: contiguous, O(1) access. Linked list: nodes, O(1) insert/delete

RC-035

  • Q: How does binary search work?
  • A: Halve search space each step — O(log n), requires sorted array

RC-036

  • Q: What is a subarray vs subsequence?
  • A: Subarray: contiguous. Subsequence: not necessarily contiguous

RC-037

  • Q: How to find the second largest element?
  • A: Single pass: track max and second max

RC-038

  • Q: What is an in-place algorithm?
  • A: Uses O(1) extra space — modifies input

RC-039

  • Q: How to merge two sorted arrays?
  • A: Two pointers, compare and place smaller — O(n+m)

RC-040

  • Q: What is a circular array?
  • A: Last element wraps to first — use modulo for indexing

RC-041

  • Q: What is an array?
  • A: A contiguous collection of elements accessed by index

RC-042

  • Q: Time complexity of array access by index?
  • A: O(1) — constant time via address calculation

RC-043

  • Q: How to reverse an array in-place?
  • A: Two pointers from ends, swap and move inward — O(n)

RC-044

  • Q: What is Kadane's algorithm?
  • A: Track current and global max subarray sum — O(n)

RC-045

  • Q: What is the two-pointer technique?
  • A: Two indices move based on conditions to avoid nested loops

RC-046

  • Q: What is a prefix sum?
  • A: Cumulative sum array for O(1) range queries

RC-047

  • Q: How to rotate an array by k positions?
  • A: Three reversals: full, first k, rest — O(n), O(1) space

RC-048

  • Q: What is the Dutch National Flag algorithm?
  • A: 3-pointer partition of 0s, 1s, 2s in single pass — O(n)

RC-049

  • Q: How to find the missing number in 1..N?
  • A: Expected sum n(n+1)/2 minus actual sum, or XOR method

RC-050

  • Q: What is the sliding window technique?
  • A: Fixed/variable window slides across array — O(n)

RC-051

  • Q: How to find the majority element?
  • A: Boyer-Moore: count cancellation — O(n), O(1) space

RC-052

  • Q: What is the time complexity of merge sort?
  • A: O(n log n) — divide-and-conquer

RC-053

  • Q: How to find duplicates in an array?
  • A: Sort and check adjacent O(n log n), or hash set O(n)

RC-054

  • Q: What is the difference between array and linked list?
  • A: Array: contiguous, O(1) access. Linked list: nodes, O(1) insert/delete

RC-055

  • Q: How does binary search work?
  • A: Halve search space each step — O(log n), requires sorted array

RC-056

  • Q: What is a subarray vs subsequence?
  • A: Subarray: contiguous. Subsequence: not necessarily contiguous

RC-057

  • Q: How to find the second largest element?
  • A: Single pass: track max and second max

RC-058

  • Q: What is an in-place algorithm?
  • A: Uses O(1) extra space — modifies input

RC-059

  • Q: How to merge two sorted arrays?
  • A: Two pointers, compare and place smaller — O(n+m)

RC-060

  • Q: What is a circular array?
  • A: Last element wraps to first — use modulo for indexing

RC-061

  • Q: What is an array?
  • A: A contiguous collection of elements accessed by index

RC-062

  • Q: Time complexity of array access by index?
  • A: O(1) — constant time via address calculation

RC-063

  • Q: How to reverse an array in-place?
  • A: Two pointers from ends, swap and move inward — O(n)

RC-064

  • Q: What is Kadane's algorithm?
  • A: Track current and global max subarray sum — O(n)

RC-065

  • Q: What is the two-pointer technique?
  • A: Two indices move based on conditions to avoid nested loops

RC-066

  • Q: What is a prefix sum?
  • A: Cumulative sum array for O(1) range queries

RC-067

  • Q: How to rotate an array by k positions?
  • A: Three reversals: full, first k, rest — O(n), O(1) space

RC-068

  • Q: What is the Dutch National Flag algorithm?
  • A: 3-pointer partition of 0s, 1s, 2s in single pass — O(n)

RC-069

  • Q: How to find the missing number in 1..N?
  • A: Expected sum n(n+1)/2 minus actual sum, or XOR method

RC-070

  • Q: What is the sliding window technique?
  • A: Fixed/variable window slides across array — O(n)

RC-071

  • Q: How to find the majority element?
  • A: Boyer-Moore: count cancellation — O(n), O(1) space

RC-072

  • Q: What is the time complexity of merge sort?
  • A: O(n log n) — divide-and-conquer

RC-073

  • Q: How to find duplicates in an array?
  • A: Sort and check adjacent O(n log n), or hash set O(n)

RC-074

  • Q: What is the difference between array and linked list?
  • A: Array: contiguous, O(1) access. Linked list: nodes, O(1) insert/delete

RC-075

  • Q: How does binary search work?
  • A: Halve search space each step — O(log n), requires sorted array

RC-076

  • Q: What is a subarray vs subsequence?
  • A: Subarray: contiguous. Subsequence: not necessarily contiguous

RC-077

  • Q: How to find the second largest element?
  • A: Single pass: track max and second max

RC-078

  • Q: What is an in-place algorithm?
  • A: Uses O(1) extra space — modifies input

RC-079

  • Q: How to merge two sorted arrays?
  • A: Two pointers, compare and place smaller — O(n+m)

RC-080

  • Q: What is a circular array?
  • A: Last element wraps to first — use modulo for indexing

RC-081

  • Q: What is an array?
  • A: A contiguous collection of elements accessed by index

RC-082

  • Q: Time complexity of array access by index?
  • A: O(1) — constant time via address calculation

RC-083

  • Q: How to reverse an array in-place?
  • A: Two pointers from ends, swap and move inward — O(n)

RC-084

  • Q: What is Kadane's algorithm?
  • A: Track current and global max subarray sum — O(n)

RC-085

  • Q: What is the two-pointer technique?
  • A: Two indices move based on conditions to avoid nested loops

RC-086

  • Q: What is a prefix sum?
  • A: Cumulative sum array for O(1) range queries

RC-087

  • Q: How to rotate an array by k positions?
  • A: Three reversals: full, first k, rest — O(n), O(1) space

RC-088

  • Q: What is the Dutch National Flag algorithm?
  • A: 3-pointer partition of 0s, 1s, 2s in single pass — O(n)

RC-089

  • Q: How to find the missing number in 1..N?
  • A: Expected sum n(n+1)/2 minus actual sum, or XOR method

RC-090

  • Q: What is the sliding window technique?
  • A: Fixed/variable window slides across array — O(n)

RC-091

  • Q: How to find the majority element?
  • A: Boyer-Moore: count cancellation — O(n), O(1) space

RC-092

  • Q: What is the time complexity of merge sort?
  • A: O(n log n) — divide-and-conquer

RC-093

  • Q: How to find duplicates in an array?
  • A: Sort and check adjacent O(n log n), or hash set O(n)

RC-094

  • Q: What is the difference between array and linked list?
  • A: Array: contiguous, O(1) access. Linked list: nodes, O(1) insert/delete

RC-095

  • Q: How does binary search work?
  • A: Halve search space each step — O(log n), requires sorted array

RC-096

  • Q: What is a subarray vs subsequence?
  • A: Subarray: contiguous. Subsequence: not necessarily contiguous

RC-097

  • Q: How to find the second largest element?
  • A: Single pass: track max and second max

RC-098

  • Q: What is an in-place algorithm?
  • A: Uses O(1) extra space — modifies input

RC-099

  • Q: How to merge two sorted arrays?
  • A: Two pointers, compare and place smaller — O(n+m)

RC-100

  • Q: What is a circular array?
  • A: Last element wraps to first — use modulo for indexing

RC-101

  • Q: What is an array?
  • A: A contiguous collection of elements accessed by index

RC-102

  • Q: Time complexity of array access by index?
  • A: O(1) — constant time via address calculation

RC-103

  • Q: How to reverse an array in-place?
  • A: Two pointers from ends, swap and move inward — O(n)

RC-104

  • Q: What is Kadane's algorithm?
  • A: Track current and global max subarray sum — O(n)

RC-105

  • Q: What is the two-pointer technique?
  • A: Two indices move based on conditions to avoid nested loops

RC-106

  • Q: What is a prefix sum?
  • A: Cumulative sum array for O(1) range queries

RC-107

  • Q: How to rotate an array by k positions?
  • A: Three reversals: full, first k, rest — O(n), O(1) space

RC-108

  • Q: What is the Dutch National Flag algorithm?
  • A: 3-pointer partition of 0s, 1s, 2s in single pass — O(n)

RC-109

  • Q: How to find the missing number in 1..N?
  • A: Expected sum n(n+1)/2 minus actual sum, or XOR method

RC-110

  • Q: What is the sliding window technique?
  • A: Fixed/variable window slides across array — O(n)

RC-111

  • Q: How to find the majority element?
  • A: Boyer-Moore: count cancellation — O(n), O(1) space

RC-112

  • Q: What is the time complexity of merge sort?
  • A: O(n log n) — divide-and-conquer

RC-113

  • Q: How to find duplicates in an array?
  • A: Sort and check adjacent O(n log n), or hash set O(n)

RC-114

  • Q: What is the difference between array and linked list?
  • A: Array: contiguous, O(1) access. Linked list: nodes, O(1) insert/delete

RC-115

  • Q: How does binary search work?
  • A: Halve search space each step — O(log n), requires sorted array

RC-116

  • Q: What is a subarray vs subsequence?
  • A: Subarray: contiguous. Subsequence: not necessarily contiguous

RC-117

  • Q: How to find the second largest element?
  • A: Single pass: track max and second max

RC-118

  • Q: What is an in-place algorithm?
  • A: Uses O(1) extra space — modifies input

RC-119

  • Q: How to merge two sorted arrays?
  • A: Two pointers, compare and place smaller — O(n+m)

RC-120

  • Q: What is a circular array?
  • A: Last element wraps to first — use modulo for indexing

RC-121

  • Q: What is an array?
  • A: A contiguous collection of elements accessed by index

RC-122

  • Q: Time complexity of array access by index?
  • A: O(1) — constant time via address calculation

RC-123

  • Q: How to reverse an array in-place?
  • A: Two pointers from ends, swap and move inward — O(n)

RC-124

  • Q: What is Kadane's algorithm?
  • A: Track current and global max subarray sum — O(n)

RC-125

  • Q: What is the two-pointer technique?
  • A: Two indices move based on conditions to avoid nested loops

RC-126

  • Q: What is a prefix sum?
  • A: Cumulative sum array for O(1) range queries

RC-127

  • Q: How to rotate an array by k positions?
  • A: Three reversals: full, first k, rest — O(n), O(1) space

RC-128

  • Q: What is the Dutch National Flag algorithm?
  • A: 3-pointer partition of 0s, 1s, 2s in single pass — O(n)

RC-129

  • Q: How to find the missing number in 1..N?
  • A: Expected sum n(n+1)/2 minus actual sum, or XOR method

RC-130

  • Q: What is the sliding window technique?
  • A: Fixed/variable window slides across array — O(n)

RC-131

  • Q: How to find the majority element?
  • A: Boyer-Moore: count cancellation — O(n), O(1) space

RC-132

  • Q: What is the time complexity of merge sort?
  • A: O(n log n) — divide-and-conquer

RC-133

  • Q: How to find duplicates in an array?
  • A: Sort and check adjacent O(n log n), or hash set O(n)

RC-134

  • Q: What is the difference between array and linked list?
  • A: Array: contiguous, O(1) access. Linked list: nodes, O(1) insert/delete

RC-135

  • Q: How does binary search work?
  • A: Halve search space each step — O(log n), requires sorted array

RC-136

  • Q: What is a subarray vs subsequence?
  • A: Subarray: contiguous. Subsequence: not necessarily contiguous

RC-137

  • Q: How to find the second largest element?
  • A: Single pass: track max and second max

RC-138

  • Q: What is an in-place algorithm?
  • A: Uses O(1) extra space — modifies input

RC-139

  • Q: How to merge two sorted arrays?
  • A: Two pointers, compare and place smaller — O(n+m)

RC-140

  • Q: What is a circular array?
  • A: Last element wraps to first — use modulo for indexing

RC-141

  • Q: What is an array?
  • A: A contiguous collection of elements accessed by index

RC-142

  • Q: Time complexity of array access by index?
  • A: O(1) — constant time via address calculation

RC-143

  • Q: How to reverse an array in-place?
  • A: Two pointers from ends, swap and move inward — O(n)

RC-144

  • Q: What is Kadane's algorithm?
  • A: Track current and global max subarray sum — O(n)

RC-145

  • Q: What is the two-pointer technique?
  • A: Two indices move based on conditions to avoid nested loops

RC-146

  • Q: What is a prefix sum?
  • A: Cumulative sum array for O(1) range queries

RC-147

  • Q: How to rotate an array by k positions?
  • A: Three reversals: full, first k, rest — O(n), O(1) space

RC-148

  • Q: What is the Dutch National Flag algorithm?
  • A: 3-pointer partition of 0s, 1s, 2s in single pass — O(n)

RC-149

  • Q: How to find the missing number in 1..N?
  • A: Expected sum n(n+1)/2 minus actual sum, or XOR method

RC-150

  • Q: What is the sliding window technique?
  • A: Fixed/variable window slides across array — O(n)

RC-151

  • Q: How to find the majority element?
  • A: Boyer-Moore: count cancellation — O(n), O(1) space

RC-152

  • Q: What is the time complexity of merge sort?
  • A: O(n log n) — divide-and-conquer

RC-153

  • Q: How to find duplicates in an array?
  • A: Sort and check adjacent O(n log n), or hash set O(n)

RC-154

  • Q: What is the difference between array and linked list?
  • A: Array: contiguous, O(1) access. Linked list: nodes, O(1) insert/delete

RC-155

  • Q: How does binary search work?
  • A: Halve search space each step — O(log n), requires sorted array

RC-156

  • Q: What is a subarray vs subsequence?
  • A: Subarray: contiguous. Subsequence: not necessarily contiguous

RC-157

  • Q: How to find the second largest element?
  • A: Single pass: track max and second max

RC-158

  • Q: What is an in-place algorithm?
  • A: Uses O(1) extra space — modifies input

RC-159

  • Q: How to merge two sorted arrays?
  • A: Two pointers, compare and place smaller — O(n+m)

RC-160

  • Q: What is a circular array?
  • A: Last element wraps to first — use modulo for indexing

RC-161

  • Q: What is an array?
  • A: A contiguous collection of elements accessed by index

RC-162

  • Q: Time complexity of array access by index?
  • A: O(1) — constant time via address calculation

RC-163

  • Q: How to reverse an array in-place?
  • A: Two pointers from ends, swap and move inward — O(n)

RC-164

  • Q: What is Kadane's algorithm?
  • A: Track current and global max subarray sum — O(n)

RC-165

  • Q: What is the two-pointer technique?
  • A: Two indices move based on conditions to avoid nested loops

RC-166

  • Q: What is a prefix sum?
  • A: Cumulative sum array for O(1) range queries

RC-167

  • Q: How to rotate an array by k positions?
  • A: Three reversals: full, first k, rest — O(n), O(1) space

RC-168

  • Q: What is the Dutch National Flag algorithm?
  • A: 3-pointer partition of 0s, 1s, 2s in single pass — O(n)

RC-169

  • Q: How to find the missing number in 1..N?
  • A: Expected sum n(n+1)/2 minus actual sum, or XOR method

RC-170

  • Q: What is the sliding window technique?
  • A: Fixed/variable window slides across array — O(n)

RC-171

  • Q: How to find the majority element?
  • A: Boyer-Moore: count cancellation — O(n), O(1) space

RC-172

  • Q: What is the time complexity of merge sort?
  • A: O(n log n) — divide-and-conquer

RC-173

  • Q: How to find duplicates in an array?
  • A: Sort and check adjacent O(n log n), or hash set O(n)

RC-174

  • Q: What is the difference between array and linked list?
  • A: Array: contiguous, O(1) access. Linked list: nodes, O(1) insert/delete

RC-175

  • Q: How does binary search work?
  • A: Halve search space each step — O(log n), requires sorted array

RC-176

  • Q: What is a subarray vs subsequence?
  • A: Subarray: contiguous. Subsequence: not necessarily contiguous

RC-177

  • Q: How to find the second largest element?
  • A: Single pass: track max and second max

RC-178

  • Q: What is an in-place algorithm?
  • A: Uses O(1) extra space — modifies input

RC-179

  • Q: How to merge two sorted arrays?
  • A: Two pointers, compare and place smaller — O(n+m)

RC-180

  • Q: What is a circular array?
  • A: Last element wraps to first — use modulo for indexing

RC-181

  • Q: What is an array?
  • A: A contiguous collection of elements accessed by index

RC-182

  • Q: Time complexity of array access by index?
  • A: O(1) — constant time via address calculation

RC-183

  • Q: How to reverse an array in-place?
  • A: Two pointers from ends, swap and move inward — O(n)

RC-184

  • Q: What is Kadane's algorithm?
  • A: Track current and global max subarray sum — O(n)

RC-185

  • Q: What is the two-pointer technique?
  • A: Two indices move based on conditions to avoid nested loops

RC-186

  • Q: What is a prefix sum?
  • A: Cumulative sum array for O(1) range queries

RC-187

  • Q: How to rotate an array by k positions?
  • A: Three reversals: full, first k, rest — O(n), O(1) space

RC-188

  • Q: What is the Dutch National Flag algorithm?
  • A: 3-pointer partition of 0s, 1s, 2s in single pass — O(n)

RC-189

  • Q: How to find the missing number in 1..N?
  • A: Expected sum n(n+1)/2 minus actual sum, or XOR method

RC-190

  • Q: What is the sliding window technique?
  • A: Fixed/variable window slides across array — O(n)

RC-191

  • Q: How to find the majority element?
  • A: Boyer-Moore: count cancellation — O(n), O(1) space

RC-192

  • Q: What is the time complexity of merge sort?
  • A: O(n log n) — divide-and-conquer

RC-193

  • Q: How to find duplicates in an array?
  • A: Sort and check adjacent O(n log n), or hash set O(n)

RC-194

  • Q: What is the difference between array and linked list?
  • A: Array: contiguous, O(1) access. Linked list: nodes, O(1) insert/delete

RC-195

  • Q: How does binary search work?
  • A: Halve search space each step — O(log n), requires sorted array

RC-196

  • Q: What is a subarray vs subsequence?
  • A: Subarray: contiguous. Subsequence: not necessarily contiguous

RC-197

  • Q: How to find the second largest element?
  • A: Single pass: track max and second max

RC-198

  • Q: What is an in-place algorithm?
  • A: Uses O(1) extra space — modifies input

RC-199

  • Q: How to merge two sorted arrays?
  • A: Two pointers, compare and place smaller — O(n+m)

RC-200

  • Q: What is a circular array?
  • A: Last element wraps to first — use modulo for indexing