Google Coding Interview Question and Answer #2: Subarray with given Sum

preview_player
Показать описание
Google Coding Interview Question and Answer #2: Subarray with given Sum.
**** Best Books For Data Structures & Algorithms for Interviews:

Watch my courses for Free on Skillshare using these links:

Given an array of non-negative integers, Find a subarray whose sum is equal to a given value.

subarray with given sum,
subarray sum equals k,
subarray with sum 0,
google interview questions,
google interview questions and answers,
google coding interview,
find subarray with given sum,

#Google #Coding #Interview
Рекомендации по теме
Комментарии
Автор

The time complexity for the 2nd solution is O(n). I forgot to mention in the video. But it's quite obvious and you would have figured it out.

KnowledgeCenter
Автор

Would it work if we moved the start < end to the while loop instead of in the if condition, so the while loop could be while(end < n and start < n)?

gpfrndw
Автор

I think there is a bug in your code. It fails for when start < end (checking more values). Consider the array [35, 1, 31] for sum 32. It will be stuck on the first 35.

gpfrndw
Автор

For array (2, 1, 1) with sum as 5 it will be in infinite loop.
Wrong solution..

decostarkumar
Автор

bro why we are decreasing from left why not from right???

anintrovert
Автор

But it not works for negative numbers.. write pseudo code for negative numbers

avinashch
Автор

bad solution this wont work for negative numbers. also a lot of bugs as other people commented.

amitavamozumder