Kadane's Algorithms for FAANG interviews

Ā·

2 min read

Let's talk about the most popular and most asked interview questions.

šŸ‘‰ š…š¢š§š š­š”šž š¦ššš±š¢š¦š®š¦ š¬š®š¦ šØšŸ šš šœšØš§š­š¢š š®šØš®š¬ š¬š®š›ššš«š«ššš².

So how do you solve this question? šŸ¤”
So many approaches are there, but the most popular approach is
using KĢ³aĢ³dĢ³aĢ³nĢ³eĢ³ā€™Ģ³sĢ³ Ģ³aĢ³lĢ³gĢ³oĢ³rĢ³iĢ³tĢ³hĢ³mĢ³

š‘ŗš’ š’˜š’‰š’‚š’• š’Šš’” š‘²š’‚š’…š’‚š’š’†ā€™š’” š’‚š’š’ˆš’š’“š’Šš’•š’‰š’ŽšŸ¤”?

šŸ‘‰ Kadane's algorithm is used to find the maximum sum of a contiguous subarray within an oĢ³nĢ³eĢ³-Ģ³dĢ³iĢ³mĢ³eĢ³nĢ³sĢ³iĢ³oĢ³nĢ³aĢ³lĢ³ Ģ³aĢ³rĢ³rĢ³aĢ³yĢ³ Ģ³oĢ³fĢ³ Ģ³nĢ³uĢ³mĢ³bĢ³eĢ³rĢ³sĢ³, which is especially useful in financial or stock market analysis, weather data processing, and similar scenarios where trends or streaks need to be identified.

šŸ‘‰ š‘Øš’š’ˆš’š’“š’Šš’•š’‰š’Ž š‘ŗš’•š’†š’‘š’”:

1. Initialize two variables:
- currentSum to track the ongoing sum of the subarray.
- maxSum to keep the maximum value found.

2. Iterate through the array:
- For each element, decide whether to add it to currentSum or start fresh with the current element (whichever is larger).
- Update maxSum if currentSum becomes greater than maxSum.
3. Return maxSum after processing all elements.

š‹šžš­'š¬ šš¢š¬šœš®š¬š¬ š­š”šž š©š¬šžš®ššØšœšØššž (š²šØš® š”šššÆšž š­šØ š¤š§šØš° š”šØš° š­šØ š°š«š¢š­šž š©š¬šžš®ššØšœšØššž ššš¬ š­š”šž š¢š§š­šžš«šÆš¢šžš°šžš« ššš¬š¤šžš š²šØš® š­šØ š°š«š¢š­šž š©š¬šžš®ššØšœšØššž).

function kadaneAlgorithm(array):

Hereā€™s the pseudocode for Kadane's Algorithm to find the maximum sum of a contiguous subarray:

Kadaneā€™s Algorithm Pseudocode

function kadaneAlgorithm(array):
    # Initialize variables
    maxSum = array[0]          # Start with the first element as max sum
    currentSum = array[0]       # Start with the first element as current sum

    # Iterate over the array starting from the second element
    for i from 1 to length(array) - 1:
        # Update currentSum by choosing the maximum between the current element 
        # or the current element added to currentSum
        currentSum = max(array[i], currentSum + array[i])

        # Update maxSum if currentSum is greater
        maxSum = max(maxSum, currentSum)

    return maxSum

š‹šžšžš­šœšØššž š©š«šØš›š„šžš¦š¬:

- https://lnkd.in/gxZn4vBM
- https://lnkd.in/gqSFpNEp
- Maximum difference of 0's and 1's in a binary string
- Maximum Sum Circular array
- Smallest sum contiguous subarray
- Largest sum increasing contiguous subarray
- Maximum Product Subarray
- Largest sum contiguous subarray with only non-negative elements.
- Largest sum contiguous subarray with unique elements.
- Maximum Alternating Sum Subarray
- Maximum Sum Rectangle In A 2D Matrix

š‘¹š’†š’‚š’ š’˜š’š’“š’š’… š’†š’™š’‚š’Žš’‘š’š’† š’”š’„š’†š’š’‚š’“š’Šš’:

Imagine tracking daily temperature changes in a city for a month. Kadaneā€™s algorithm could help you identify the warmest streak of consecutive days.

IĢ³ Ģ³hĢ³oĢ³pĢ³eĢ³ Ģ³yĢ³oĢ³uĢ³ Ģ³lĢ³iĢ³kĢ³eĢ³ Ģ³tĢ³hĢ³eĢ³ Ģ³eĢ³xĢ³pĢ³lĢ³aĢ³nĢ³aĢ³tĢ³iĢ³oĢ³nĢ³ Ģ³aĢ³nĢ³dĢ³ Ģ³uĢ³nĢ³dĢ³eĢ³rĢ³sĢ³tĢ³aĢ³nĢ³dĢ³iĢ³nĢ³gĢ³ Ģ³oĢ³fĢ³ Ģ³AĢ³lĢ³gĢ³oĢ³rĢ³iĢ³tĢ³hĢ³mĢ³sĢ³
Ģ³
Ģ³FĢ³oĢ³lĢ³lĢ³oĢ³wĢ³ Ģ³aĢ³nĢ³dĢ³ Ģ³cĢ³oĢ³nĢ³nĢ³eĢ³cĢ³tĢ³ Ģ³fĢ³oĢ³rĢ³ Ģ³mĢ³oĢ³rĢ³eĢ³ Ģ³cĢ³oĢ³dĢ³iĢ³nĢ³gĢ³ Ģ³aĢ³nĢ³dĢ³ Ģ³WĢ³eĢ³bĢ³ Ģ³dĢ³eĢ³vĢ³eĢ³lĢ³oĢ³pĢ³mĢ³eĢ³nĢ³tĢ³ Ģ³pĢ³oĢ³sĢ³tĢ³sĢ³ Ģ³ Ravi Kumar

Did you find this article valuable?

Support Ravi Kumar by becoming a sponsor. Any amount is appreciated!

Ā