Split an Array into Equal Sum Subarrays (with Google Software Engineer)

  Рет қаралды 21,778

Exponent

Exponent

Күн бұрын

Пікірлер: 46
@tryexponent
@tryexponent 10 ай бұрын
Don't leave your software engineering career path to chance. Make sure you're interview-ready with Exponent's software developer interview prep course: bit.ly/3ztsYPt
@massimomonticelli6010
@massimomonticelli6010 2 жыл бұрын
Thanks for the video! Really appreciated. Assuming non negative numbers in the list, the loop can also exit - returning None - if the running sum at some point gets greater than the target. Thanks again, Massimo.
@stevefoo13
@stevefoo13 2 жыл бұрын
Kool solution. I came up with another one to use two pointers: one at the start of the list and one at the end of list. As you increment and decrement each pointer you add up the sum in two different variables. You would stop when each sum is equal to each other and the start poiner is sequential to the end pointer. If the pointers ever equal one another then there is no equal sub-array.
@unwanted_spam
@unwanted_spam 2 жыл бұрын
Same Chad
@kunaljain6932
@kunaljain6932 2 жыл бұрын
You didn't mention that when to increment or decrement it would be nice if you can share that code i guess it's not gonna work in some cases
@kunaljain6932
@kunaljain6932 2 жыл бұрын
1,3,2,-8,3,5,-10
@kunaljain6932
@kunaljain6932 2 жыл бұрын
This this and should fail if i am thinking same as you were
@kunaljain6932
@kunaljain6932 2 жыл бұрын
ANS :: 0 to 3 and 4 to 7
@shenjun5631
@shenjun5631 2 жыл бұрын
The solution is dependent on if the array is sorted?
@massimomonticelli6010
@massimomonticelli6010 2 жыл бұрын
No, the array doesn’t need to be sorted.
@xugu624
@xugu624 20 күн бұрын
What company would ask such easy questions which can be done within 2 minutes?
@latenightjamming2255
@latenightjamming2255 8 ай бұрын
Hi team I guess this is wrong solution, with input [1,3,2] this will fail as he is trying only to put continuous elements. Thats why his [1,2,3] worked but [1,3,2] will not.
@yiannig7347
@yiannig7347 8 ай бұрын
I found it amusing that he preached about mastering the programming language for interviews, yet stumbled over range functions.
@Sunny-wi4wm
@Sunny-wi4wm 2 жыл бұрын
Please correct me if I am wrong, I don't think the solution is correct. Does it work for 1,3,3,5 even if it is sorted? I believe it would return None instead of [1,5] [3,3]. May be I missed something.
@anand_undavia
@anand_undavia 2 жыл бұрын
[1,5] is not a subarray. It is a subsequence. A subarray for an array is kind of like a substring for a string.
@Sunny-wi4wm
@Sunny-wi4wm 2 жыл бұрын
@@anand_undavia That makes a lot of sense now. Thank you
@r.006
@r.006 2 жыл бұрын
@@anand_undavia great explanation, I had the same question
@manantank
@manantank 2 жыл бұрын
They should clarify what subarray means. I was also under the impression that subarray is a subsequence
@kunaljain6932
@kunaljain6932 2 жыл бұрын
Yep both of them ignored that
@linshengred
@linshengred Жыл бұрын
Is this interview result will be hire / lean hire or strong hire?
@jacobsimon4699
@jacobsimon4699 2 жыл бұрын
Nice work! 🔥
@Boringpenguin
@Boringpenguin 2 жыл бұрын
A related but way harder problem would be to find any two integer subsequence (not necessarily just splitting the original one up) with equal sum e.g. a = [4, 2, 12, 5, 3, 21], the two subarrays would be [2, 12] and [3, 21], with sum = 24
@unwanted_spam
@unwanted_spam 2 жыл бұрын
Bro 2,12 is not even 24 Your brain tricked you into thinking 2*12=2+12
@carefree_ladka
@carefree_ladka 2 ай бұрын
Google would never ask such questions. I mean it's simple to code .
@r.006
@r.006 2 жыл бұрын
Why do we assume the list is sorted? Good solution otherwise
@massimomonticelli6010
@massimomonticelli6010 2 жыл бұрын
We just don’t assume it is sorted.
@latenightjamming2255
@latenightjamming2255 8 ай бұрын
I have tried writing the whole code which is bit more generic and not only for 2 splits but also for k number of splits. def split_array(inp_arr, st_ind, end_ind) base_array_st = [] base_array_end = [] base_array_st = inp_arr[0...st_ind] if st_ind > 0 derived_array = inp_arr[st_ind..end_ind] base_array_end = inp_arr[(end_ind+1)...(inp_arr.length)] if end_ind < (inp_arr.length-1) puts "---splitting----inp_arr----#{inp_arr.inspect}--into----#{derived_array.inspect}---#{(base_array_st + base_array_end).inspect}" # Heree dervied_Array is to be transfered return derived_array, (base_array_st + base_array_end) end def dfs(curr_sub_arrs, k, target_sum, sub_arr_ind = 0) puts "---came inside----dfs----#{curr_sub_arrs.inspect}-----target_sum: #{target_sum}--sub_arr_ind:-#{sub_arr_ind}" sub_arr_to_operate = curr_sub_arrs[sub_arr_ind] if sub_arr_ind < (k-1) value_with_current_subarr = dfs(curr_sub_arrs, k, target_sum, (sub_arr_ind+1)) if sub_arr_to_operate.sum == target_sum return true if value_with_current_subarr else return sub_arr_to_operate.sum == target_sum end curr_sub_arr_length = sub_arr_to_operate.length curr_diff = 1 (1...curr_sub_arr_length).each do |total_elements_to_shift| curr_sub_arr_length.times do |st_ind| end_ind = st_ind + total_elements_to_shift - 1 temp_arr = sub_arr_to_operate derived_array, base_array = split_array(sub_arr_to_operate, st_ind, end_ind) next if base_array.sum != target_sum new_curr_sub_arrays = curr_sub_arrs.clone new_curr_sub_arrays[sub_arr_ind] = base_array new_curr_sub_arrays[sub_arr_ind+1] = derived_array + curr_sub_arrs[sub_arr_ind+1] return true if dfs(new_curr_sub_arrays, k, target_sum, (sub_arr_ind+1)) end end return false end def divide_arr(arr, k) puts "--input---#{arr.inspect}" return -1 if arr.nil? || k.nil? || arr.length < k total_sum = arr.sum return -1 if (total_sum % k) != 0 sum_of_each_subarr = total_sum/k starting_sub_arrs = [] temp_arr = arr (1...k).each do |arr_ind| new_val = temp_arr.pop starting_sub_arrs
@technovinitySystem
@technovinitySystem 2 жыл бұрын
will this solution work in case of this [1, 3, 7, 2 , 11] ?
@phanindra5580
@phanindra5580 2 жыл бұрын
the output for this is None because a subarray is a contiguous part of array.
@kharljohnrayala
@kharljohnrayala 2 жыл бұрын
won't work with these two samples: sample 1 = [1,4,4,5,6,6,10] sample 2 = [1,2,5,6,12] because the correct subarrays for my examples don't have consecutive indexes from original arrays
@vimalvaira
@vimalvaira 2 жыл бұрын
in both these two sample the result should be none, why exactly will this not work?
@phanindra5580
@phanindra5580 2 жыл бұрын
A subarray is a contiguous part of array, when array doesn't have contiguous indexes it can't be termed as subarray.
@parthgabhane1419
@parthgabhane1419 2 жыл бұрын
At first i thought the left person is a girl
@shenth27
@shenth27 2 жыл бұрын
You're not alone bro 😄
@YTGhostCensorshipCanSuckMe
@YTGhostCensorshipCanSuckMe 2 жыл бұрын
at first I thought the right person was a girl.
@sfsdeniso5941
@sfsdeniso5941 2 жыл бұрын
we've all been there :)
@cnkumar20
@cnkumar20 2 жыл бұрын
also don't forget asking the interviewer if they prefer prefer him/he/she/her/them/they
@someshsangwan3768
@someshsangwan3768 2 жыл бұрын
lol this is a basic question .
Microsoft Software Engineering Interview: Number Array
23:36
Exponent
Рет қаралды 21 М.
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 2,8 МЛН
Players vs Pitch 🤯
00:26
LE FOOT EN VIDÉO
Рет қаралды 133 МЛН
ТЫ В ДЕТСТВЕ КОГДА ВЫПАЛ ЗУБ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 4,5 МЛН
Software Engineer Mock Interview: 3 Sum Algorithm Problem
24:33
how Google writes gorgeous C++
7:40
Low Level
Рет қаралды 951 М.
Coding Interview | Software Engineer @ Bloomberg (Part 1)
30:05
Keep On Coding
Рет қаралды 4,7 МЛН
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 2,8 МЛН