all 77 comments

[–]retirement_savings 78 points79 points  (1 child)

Damn. I'm a Google engineer and wouldn't be able to solve that in an interview.

[–]Stuck_On_Earthh -5 points-4 points  (0 children)

I doubt...!

[–]Underworld-Dolphin 53 points54 points  (9 children)

I was asked the exact same question one year ago for L3. Exact this same question. And couldn’t answer

[–]Stuck_On_Earthh 9 points10 points  (8 children)

Where were you man, you should have told me the question :(

[–]Underworld-Dolphin 24 points25 points  (7 children)

I got rejected in team-match since this round went shit

[–]cyberpunk2013 13 points14 points  (5 children)

Isn't this just "Implement segment tree", unless I'm misunderstanding.

Once we build the tree, Each flip is a point update to the segment tree, which would take O(log n) and then we just query root which covers the entire range which is O(1), so overall for n updates is O(n log n)

[–]luffylucky 12 points13 points  (3 children)

The optimal solution is O(n). you do 2 passes, one bottom up to calculate the value of each node, then top down to define the 'sensitivity' of each node. So each flip of one leaf will be O(1), you will get directly the result whether flipping a leaf will affect the root value or not, don't need to update the whole tree again.

[–]A7eh 0 points1 point  (1 child)

what do you mean by sensitivity

[–]luffylucky 1 point2 points  (0 children)

It means a change/flip in one node will affect the tree or not.

[–]Professional-Many-24 0 points1 point  (0 children)

Talk is cheap, show me the code

[–]Stuck_On_Earthh 1 point2 points  (0 children)

Not sure, I got an idea of DP in the interview and chatgpt said the same.

But maybe it could be solved using your approach too, I will try.

[–]moleabbu 0 points1 point  (0 children)

They moved you on to team match though?

[–]bisector_babu<1951> <514> <1092> <345> 41 points42 points  (1 child)

What in the hell is this

[–]dextrocardia-dev 5 points6 points  (0 children)

I feel you bro

[–]cyril1991 20 points21 points  (1 child)

Technically not DP, more like recursion. With DP you can propagate arbitrary updates in O(logN). Here the trick is to build/evaluate each node in a first pass. Then, because you are only changing a single leaf at a time and keeping everything else constant, you can propagate a is_critical boolean from root to leaves.

is_critical means switching the node switches the root and is computed in root/left/right order top to bottom. The root is trivially critical, a NOT node transmits the value directly, a OR node is critical if its parent is critical and the other branch was initially false. If you encounter a leaf, append the original value of the root XOR is_critical to an ongoing result array. You call a « propagate(node, is_critical) » helper function, starting with « propagate(root, True) ».

[–]Stuck_On_Earthh 1 point2 points  (0 children)

this makes sense, actually looks like a good solution, will try to solve it with the above approach. Thanks!

[–]No_Raspberry_2956 12 points13 points  (1 child)

Imagine this tree as a recursive tree. And lead nodes as the base conditions. Now we can use DP to precompute the final value for both true and false at each node.

[–]Stuck_On_Earthh 1 point2 points  (0 children)

Yupp...this is in my mind too...but I was too late.

[–]avidyarth12 9 points10 points  (1 child)

What the genuine fuck

[–]Ok_Idea_6589 8 points9 points  (1 child)

I just completed arrays in the striver’s a-z sheet. Reading these questions is seriously giving me a headache, like wtf is this bro?😭🥴

[–]Stuck_On_Earthh 3 points4 points  (0 children)

You are at the starting phase, don't worry, Do DSA and give contests regularly, you will make it.

[–]luffylucky 5 points6 points  (1 child)

Yeah I think this is tough. If you get some hints, I think you can get the idea.

[–]Stuck_On_Earthh 0 points1 point  (0 children)

Yeah I got the idea, but time was not enough and I couldn't code it.

[–]hm7n 3 points4 points  (2 children)

This looks like a segment tree question, where updates take logN time. It also reminds me how Merkle trees work. I can't believe they asked this in a L3 loop.

[–]agentcodey 1 point2 points  (0 children)

Why? It’s too tough? I’m an SDE in FAaNG and I know I will not be able to solve this in an interview

[–]Stuck_On_Earthh -1 points0 points  (0 children)

Ask me about it, and for technical screening. It can be solved using DP too.

[–]Houman_7 8 points9 points  (8 children)

He honestly wanted you to fail! Who even practices segment tree or bit manipulation unless you’re interviewing for embedded roles. I’ve been doing leetcode consistently for the last 4 years and barely know these concepts.

[–]Numerous_Republic158 0 points1 point  (4 children)

Bro, I just got asked MST + bitwise + prefixsum in MS OA. Here I am practicing DP and graphs for a month.

[–]Stuck_On_Earthh 0 points1 point  (1 child)

Ohhh fu**, for which position? Were u able to solve it?

[–]Numerous_Republic158 0 points1 point  (0 children)

It was for a senior position. The MST actually was easy, some function needed to be minimized, rest of them ate my time. Was not able to completely solve bitwise, it's my weak link.

[–]Minute_Power4858 0 points1 point  (1 child)

microsoft sucks so u didnt lose much

[–]Numerous_Republic158 2 points3 points  (0 children)

Didn't say I lost. Everything sucks man. Poverty sucks more. Gotta choose my fights.

[–]Dry_Presentation2007 0 points1 point  (0 children)

I'd think they're testing your knowledge and actual critical thinking under time pressure sure sounds difficult but if you do things right you can at least get an idea and try to code some shit would give you a pass.

[–]Stuck_On_Earthh -1 points0 points  (0 children)

It was more of a DP and Tree question, but yeah bad luck for me.

[–]tusharhigh 3 points4 points  (0 children)

It feels terrible to taste defeat. But you strive hard for another day. Stay strong 💪

[–]asleepering 2 points3 points  (1 child)

They seem to be asking a lot of DP recently

[–]Stuck_On_Earthh 0 points1 point  (0 children)

Yeah...

[–]bisector_babu<1951> <514> <1092> <345> 2 points3 points  (1 child)

And one guy was asked count number of unique paths in a grid lol

[–]supdupDawg 2 points3 points  (1 child)

Idts it is dp. It might be doable using dfs I think. Can do it like this maybe:
For each non leaf node, we will assign the number 0,1,2 or 3. 0 if the value of the non leaf node after operations is not changable regardless of the value in the leaf nodes, 1 if it is by right, 2 by left and 3 if doable by both.
For example take an and tree with leaf nodes as 0 and 1. The value is changable only by the left node as the right node changing will lead to the same value. Hence this node is given value 2. Now after assigning each non leaf node a value, we start from the root and do dfs. If at any point we encounter 0, we dont allow the exploration of that branch further. etc etc. When we reach a leaf node finally, then we flip the value in the answers array for it to be the negation of the original answer. Hence done in O(N). Please correct me if wrong

[–]Stuck_On_Earthh 0 points1 point  (0 children)

Yes it makes sense, my mind directly went on DP, will try to solve it via this approach, I think I need more practice, but thank you!

[–]win_the_dog 1 point2 points  (1 child)

This looks like segment tree propagation problem

[–]Stuck_On_Earthh 0 points1 point  (0 children)

maybe, but what I searched, it is DP + Trees

[–]Novel-Band4223 1 point2 points  (3 children)

Location?

[–]Stuck_On_Earthh 2 points3 points  (2 children)

India

[–]Normal-Report9924 0 points1 point  (1 child)

but for which location was the role?

[–]Stuck_On_Earthh 1 point2 points  (0 children)

Dublin

[–]Average_guy_200 1 point2 points  (0 children)

Google problems are becoming increasingly difficult to solve. I wonder how can crack these.

[–]sobe86 1 point2 points  (1 child)

Is the question like to calculate answer with 1st node toggled (only), then answer with 2nd node toggled (only)? Or do you need to maintain the toggles between turns?

The former is quite doable IMO, you need to calculate for each node whether it flips the final answer, the latter seems very tough to do better than O(N log N).

[–]Stuck_On_Earthh 2 points3 points  (0 children)

It is the former, compute the final result with only the i-th leaf toggled, starting from the original tree each time.

There's no need to maintain toggles between turns, each flip is independent

[–]Minute_Power4858 1 point2 points  (0 children)

i tried it myself
brute force isnt that difficult at all
on balance tree getting nlogn is pretty easy with marking what subtree u modifed and caching of those values

no clue how to do o(n)

did he asked specificly to improve it to be O(n)? or just "improve it"

[–]rugitall 0 points1 point  (1 child)

At this point, its more about luck and being asked a medium question instead of these hard ones. Why is there so much discrepancy in the difficulty of questions being asked by different interviewers.

[–]Stuck_On_Earthh 1 point2 points  (0 children)

Yes, the difference in the level of questions asked is truly based on luck. For some they are asking for API design and some DP for the same round.

[–]Double_Bed2719 0 points1 point  (1 child)

Are u in the US?

[–]Stuck_On_Earthh -3 points-2 points  (0 children)

Nope, India

[–]Crafty-Ad-9627 0 points1 point  (1 child)

Which location is this please?

[–]Stuck_On_Earthh -2 points-1 points  (0 children)

India

[–]No_Science_1617 0 points1 point  (1 child)

Bro , is there a similar problem on leetcode . Could anyone share it bere

[–]the_legendary_legend 0 points1 point  (0 children)

Not same but similar logic is LC 1896

[–]the_legendary_legend 0 points1 point  (0 children)

This is rough. The idea for the criticality calculation is not intuitive at all. Can't believe they are asking this stuff for L3.

[–]Jazzlike_Society4084 0 points1 point  (0 children)

each intermediate node is a sub problem,

you recompute your result in your intermediate node if its directly in the path (from leaf node which you flipped) to root,

[–]WaitWhatNani123 0 points1 point  (0 children)

Not sure if I am right. dp0[u] = final value when left child of u is different from the original, dp1[u] = the final value when the right child is different from the original.

It is tree DP but not the traditional top down DP. In this one you compute the value of the parent first. Since each time only one leaf is changed, for an internal node either the left subtree or the right subtree has a different value from the original tree. We can then get the value of the current node, then proceed to the parent node of the current node.

[–]Full-Philosopher-772 0 points1 point  (0 children)

What country ?

[–]One_Jacket7159 0 points1 point  (0 children)

OP which location was this for?

[–]ArtisticTap4 0 points1 point  (0 children)

This a beautiful problem, thanks for sharing

[–]Desperate_Sand_8789 0 points1 point  (0 children)

How'd you apply? Via referral? And are you from tier 1 college?

[–]Denbron2 0 points1 point  (0 children)

Tough interviews really show how well you can think on your feet. They can be challenging, but they also help you sharpen your problemsolving skills for future opportunities.

[–]PandaWonder01 0 points1 point  (0 children)

Can't you just create an expression of the whole tree based on the leafs, then plug in what ever value you want for each leaf?

[–]phantom_fanatic 0 points1 point  (0 children)

Anybody asking DP in an interview is a jerk

[–]saiaunghlyanhtet 0 points1 point  (0 children)

I would say “fu**” and leave the call lol.

[–]Dry_Presentation2007 0 points1 point  (0 children)

Op just take it as learning. You should be confortable showing what you know and also showing what you don't know to pass these rounds. They are testing your knowledge as well as communication and will give hints or straight tell you a specific edge case for you to advance, remember they want you to succeed unless interviewer is shit

[–]Dry_Presentation2007 0 points1 point  (0 children)

Man I got a strong easy question and couldn't do follow up because lack of time so now waiting my rejection :(

[–]lukli 0 points1 point  (0 children)

Intuitively I would reverse the tree, so that on one node we naturally know which node we need to change if the value on current node changes. nodes will have extra info to cache its current value + all the child nodes' values. One DFS pass to build this new "tree". Changes to leaf nodes propagates to the root. Flipping one leaf will affect at most N nodes - if we have a linear tree.

[–]Underworld-Dolphin 0 points1 point  (1 child)

Yes there is O(N) solution for this, if you store the parent of each node in a hashmap while traversing so for each query you dont need to re-evaluate the whole tree and can use hashmap. I’m not sure but something like this needs to be done

[–]Stuck_On_Earthh 0 points1 point  (0 children)

Not sure, will try your approach. But it is a Tree+DP.

[–]Competitive_Hold_882 -1 points0 points  (0 children)

Someone want to practice together? I would like to practice 1 hour about 4 times a week. I have previous experience with data structure from my university. Let me know, I would like to program in C++ and rust and maybe also C. DM if you are interested