Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Kadane's Algorithm Maximum Subarray in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kadane's Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of Kadane's algorithm on this array?
Given the array [-2, 1, -3, 4, -1, 2, 1, -5, 4], what is the maximum subarray sum found by Kadane's algorithm?
DSA Typescript
function maxSubArray(nums: number[]): number {
  let maxSoFar = nums[0];
  let maxEndingHere = nums[0];
  for (let i = 1; i < nums.length; i++) {
    maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
    maxSoFar = Math.max(maxSoFar, maxEndingHere);
  }
  return maxSoFar;
}

console.log(maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4]));
A6
B5
C7
D4
Attempts:
2 left
💡 Hint
Think about the continuous subarray with the largest sum.
🧠 Conceptual
intermediate
1:30remaining
What does Kadane's algorithm track during iteration?
Kadane's algorithm uses two variables during iteration over the array. What do these variables represent?
ACurrent maximum subarray sum ending at current index and overall maximum subarray sum found so far
BSum of all positive numbers and sum of all negative numbers
CMaximum element in the array and minimum element in the array
DLength of the longest subarray and sum of the longest subarray
Attempts:
2 left
💡 Hint
One variable resets when sum becomes negative, the other keeps track of the best result.
🔧 Debug
advanced
2:00remaining
Identify the error in this Kadane's algorithm implementation
What error does this code produce when run on array [-1, -2, -3]? function maxSubArray(nums: number[]): number { let maxSoFar = 0; let maxEndingHere = 0; for (let i = 0; i < nums.length; i++) { maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]); maxSoFar = Math.max(maxSoFar, maxEndingHere); } return maxSoFar; } console.log(maxSubArray([-1, -2, -3]));
AReturns 0 incorrectly
BReturns -1 incorrectly
CReturns 3 correctly
DThrows a runtime error
Attempts:
2 left
💡 Hint
Check initial values of maxSoFar and maxEndingHere.
❓ Predict Output
advanced
2:00remaining
What is the maximum subarray sum for all negative numbers?
What does Kadane's algorithm return for the array [-4, -2, -7, -3]?
DSA Typescript
function maxSubArray(nums: number[]): number {
  let maxSoFar = nums[0];
  let maxEndingHere = nums[0];
  for (let i = 1; i < nums.length; i++) {
    maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
    maxSoFar = Math.max(maxSoFar, maxEndingHere);
  }
  return maxSoFar;
}

console.log(maxSubArray([-4, -2, -7, -3]));
A-3
B-2
C-4
D0
Attempts:
2 left
💡 Hint
Kadane's algorithm returns the largest single element if all are negative.
🚀 Application
expert
3:00remaining
How to modify Kadane's algorithm to return the subarray itself?
Which modification allows Kadane's algorithm to return the actual maximum sum subarray, not just the sum?
AUse a stack to keep track of elements and pop when sum is negative
BStore all subarrays and pick the one with maximum sum at the end
CSort the array first and then apply Kadane's algorithm
DTrack start and end indices when maxEndingHere resets and update them when maxSoFar changes
Attempts:
2 left
💡 Hint
Keep track of where the current subarray starts and update when you find a better sum.

Practice

(1/5)
1. What does Kadane's Algorithm primarily help to find in an array of integers?
easy
A. The number of positive elements
B. The minimum element in the array
C. The total sum of all elements
D. The maximum sum of a contiguous subarray

Solution

  1. Step 1: Understand the problem Kadane's Algorithm solves

    Kadane's Algorithm is designed to find the maximum sum of any contiguous subarray within a given array.
  2. Step 2: Differentiate from other array operations

    It does not find minimum elements, total sum, or count positives, but specifically the largest sum of a continuous segment.
  3. Final Answer:

    The maximum sum of a contiguous subarray -> Option D
  4. Quick Check:

    Maximum subarray sum = The maximum sum of a contiguous subarray [OK]
Hint: Kadane finds max sum in continuous parts of array [OK]
Common Mistakes:
  • Confusing max sum with total sum of array
  • Thinking it finds minimum values
  • Assuming it counts positive numbers
2. Which of the following is the correct initialization step in Kadane's Algorithm in TypeScript?
easy
A. let maxSoFar = Number.MIN_SAFE_INTEGER, maxEndingHere = 0;
B. let maxSoFar = [], maxEndingHere = [];
C. let maxSoFar = 0, maxEndingHere = 0;
D. let maxSoFar = null, maxEndingHere = null;

Solution

  1. Step 1: Understand initialization for Kadane's Algorithm

    We initialize maxSoFar to the smallest safe integer to handle arrays with negative numbers correctly, and maxEndingHere to 0.
  2. Step 2: Check each option's correctness

    let maxSoFar = Number.MIN_SAFE_INTEGER, maxEndingHere = 0; correctly uses Number.MIN_SAFE_INTEGER for maxSoFar and 0 for maxEndingHere. Other options use incorrect types or values.
  3. Final Answer:

    let maxSoFar = Number.MIN_SAFE_INTEGER, maxEndingHere = 0; -> Option A
  4. Quick Check:

    Initialization with min safe integer = let maxSoFar = Number.MIN_SAFE_INTEGER, maxEndingHere = 0; [OK]
Hint: Initialize maxSoFar with smallest integer to handle negatives [OK]
Common Mistakes:
  • Initializing maxSoFar to 0 causes wrong results with all negatives
  • Using arrays instead of numbers for maxSoFar
  • Setting variables to null instead of numbers
3. What is the output of the following TypeScript code using Kadane's Algorithm?
function maxSubArray(nums: number[]): number {
  let maxSoFar = Number.MIN_SAFE_INTEGER;
  let maxEndingHere = 0;
  for (const num of nums) {
    maxEndingHere = Math.max(num, maxEndingHere + num);
    maxSoFar = Math.max(maxSoFar, maxEndingHere);
  }
  return maxSoFar;
}

console.log(maxSubArray([-2,1,-3,4,-1,2,1,-5,4]));
medium
A. 6
B. 7
C. 5
D. 4

Solution

  1. Step 1: Trace the Kadane's Algorithm on the input array

    Iterate through [-2,1,-3,4,-1,2,1,-5,4]: max subarray sums update as -2,1, -2,4,3,5,6,1,5.
  2. Step 2: Identify the maximum sum found

    The maximum sum found during iteration is 6, which corresponds to subarray [4,-1,2,1].
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Max subarray sum = 6 [OK]
Hint: Track maxEndingHere and maxSoFar updates carefully [OK]
Common Mistakes:
  • Adding all positive numbers only
  • Ignoring negative numbers impact
  • Confusing maxSoFar with maxEndingHere
4. Identify the bug in this Kadane's Algorithm implementation and select the fix:
function maxSubArray(nums: number[]): number {
  let maxSoFar = 0;
  let maxEndingHere = 0;
  for (const num of nums) {
    maxEndingHere = Math.max(num, maxEndingHere + num);
    maxSoFar = Math.max(maxSoFar, maxEndingHere);
  }
  return maxSoFar;
}
medium
A. Remove resetting maxEndingHere to 0 when negative
B. Initialize maxSoFar with Number.MIN_SAFE_INTEGER instead of 0
C. Change maxEndingHere += num to maxEndingHere = num
D. Return maxEndingHere instead of maxSoFar

Solution

  1. Step 1: Analyze initialization problem

    Initializing maxSoFar to 0 causes wrong results if all numbers are negative, as maxSoFar never updates below 0.
  2. Step 2: Fix initialization to handle negative arrays

    Setting maxSoFar to Number.MIN_SAFE_INTEGER allows correct tracking of max sums even if all are negative.
  3. Final Answer:

    Initialize maxSoFar with Number.MIN_SAFE_INTEGER instead of 0 -> Option B
  4. Quick Check:

    Proper initialization fixes negative-only arrays [OK]
Hint: Use smallest integer for maxSoFar to handle negatives [OK]
Common Mistakes:
  • Leaving maxSoFar as 0 causes wrong max for negatives
  • Removing reset of maxEndingHere breaks algorithm logic
  • Returning wrong variable from function
5. Given an array of integers, how can Kadane's Algorithm be adapted to also return the start and end indices of the maximum sum subarray?
Choose the correct approach.
hard
A. Use two nested loops to find max sum and indices
B. Store all subarrays and pick the max sum one at the end
C. Track start index when maxEndingHere resets; update end index when maxSoFar updates
D. Return the first positive element's index as start and end

Solution

  1. Step 1: Understand Kadane's Algorithm extension for indices

    When maxEndingHere resets to current element, update start index; when maxSoFar updates, update end index to current position.
  2. Step 2: Compare with other approaches

    Storing all subarrays or using nested loops is inefficient; returning first positive index is incorrect.
  3. Final Answer:

    Track start index when maxEndingHere resets; update end index when maxSoFar updates -> Option C
  4. Quick Check:

    Index tracking with resets and updates = Track start index when maxEndingHere resets; update end index when maxSoFar updates [OK]
Hint: Update start on reset, end on max update for indices [OK]
Common Mistakes:
  • Using nested loops loses Kadane's efficiency
  • Ignoring index updates when maxEndingHere resets
  • Assuming first positive element is max subarray start
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard