The trapping rain water problem can be solved efficiently using a two-pointer approach that maintains leftMax and rightMax variables to track the tallest buildings encountered from each side, processing the smaller side first to calculate water accumulation as the difference between the maximum height and current height, achieving O(N) time complexity and O(1) space complexity.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Goldman Sachs 1st round experience Java DeveloperAdded:
So login.
>> No, no, just just wait. Uh it will load.
Yeah.
>> Okay.
Okay. Uh, let me keep just give me a minute.
>> Mhm. Okay.
Okay. So, uh I'll be giving you uh >> basically the first question.
>> Okay.
>> And after that we will have another uh exercise.
>> Okay. Um Yeah.
>> So if you see from line number five till 9 you have the instructions. If you have any question you can let me know.
Okay, just consider doing some additional test.
And you will have total of like uh 1 hour to complete those two questions.
>> Okay.
>> Yeah.
>> So from line 24 you can start the implementation.
>> Mhm. Okay.
corrector return zero answer. Yeah.
So firstly I explain uh the the question.
>> Yeah.
>> Yeah. So first first correct does not repeat anywhere.
[clears throat] Yeah. So like uh there uh I uh I will use hashmap to store like frequency of uh each character and like uh then uh I uh then um first I count all the character then I iterate again to find the first character whose like frequency is one.
>> Okay.
>> Yeah. And uh and then like uh uh for ensure like yeah sorry >> can I write >> repeat you have some of our network issue?
Okay.
Now, am I audible?
>> Hello.
>> Let's uh >> yeah. So like um in this code firstly like I uh I'm using hashmap to store like frequency of each character and after that uh find uh firstly like uh I count all character then I iterate again to find um find the first character whose frequency we can say like only one.
>> Okay.
>> Yeah. So can I write uh logic?
>> Yeah. Yeah. Go ahead.
>> [snorts] [snorts] >> Yes. U so it is working fine.
Hello.
>> Okay. Cool. Uh just can you run me through the solution once?
>> Uh yeah sure. So like um firstly like um uh um we can say this one method find first method we can say like I'm creating one has map. So there like we are uh uh storing character in integer inside it and after that like value we can say like uh how how many times like uh it appears we can say.
So after that like we are applying one for loop here. So like I am uh uh I am uh converting a string into care array for uh for each uh character and after that like uh uh yeah so after that like I am um adding inside the f yeah so like there like um uh I'm using uh uh yeah so like there um inside mapper uh if um we don't get so that time we are passing default value like uh we can say like if character not present then uh we will take zero.
>> Okay.
>> Otherwise like um uh and uh it increase by one uh count we can say with the help of this.
>> Okay.
>> Yeah. And after that like uh put method.
So like put a we can say like update value in map this one.
>> Mhm.
>> Yeah.
>> Mhm.
>> And after that I am againing I'm using for loop. So like sec second for loop we are using for mainly like maintain order and we again go from like u uh left to right.
>> Mhm.
And uh then uh and then like we are checking the frequency like equals to 1.
There we are checking frequency. And after that like this character appear like only once then uh return u we can say character.
>> Okay.
Okay. Got it.
So iterating over the loop two times, right?
>> Okay.
So you're first putting it within a map and then for whichever value it is one you're just where is the second iteration >> only one iteration is there this one and second one is like uh we are fetching the value >> got it but uh is it On map is it stored in ordered or unordered?
>> Map. Hashmap.
>> Mhm.
>> So hashmap.
>> Okay. You have used linked hashmap.
>> Linked hashmap. Yeah. So it is maintain ordered.
>> Okay. Okay. Okay. Got it. Got it. Uh okay. Let me [snorts] share the next one.
>> [snorts] >> Okay, just commenting this code one here. Okay.
Yeah.
So this is from here uh from line number 71 you have this new thing >> uh the new access question and from 74 till 96 is the instructions for the problem. So you can just go through it once >> and let's discuss them once you have gone through it.
>> Okay.
>> Okay. Okay.
form 71.
Okay.
Don't do that.
My house.
So, So uh um here like uh giving one array like 0 1 3 0 1 2 this one and uh uh each number like uh represents the height of a building.
>> Yeah. Yes. Yes.
Yes.
>> Okay.
And uh and like um we can say like uh after uh like snow.
>> Yeah. Where it will get accumulated.
>> Mhm.
>> So if you see between the span uh three >> and towards right there is four right. M >> so on zero it will store three levels on one it will store only two levels >> and on two it will store one level and the zero it will store three levels >> right >> because between the two uh buildings like that's as like if it goes beyond three it will kind of overflow on the left side right >> Mhm.
So that's that's how you have to find like how much it will get captured between all the blocks.
>> Mhm.
>> Okay. So like uh okay.
>> Mhm.
So they are like um [clears throat] >> Mhm.
>> just what I'm thinking. Yeah. So like um we can say u at every index like we need like uh uh tallest on left and tallest on right.
>> Okay.
>> And then like u but uh we can say like uh calculating like again and again so like it will give slow performance.
>> Okay.
>> Yeah. So uh so like uh I can use for this uh two point uh two pointer from both ends.
>> Okay. Okay. M so like um so like we I can take uh two variables like uh uh left max and right max and like uh at um at each step like I process the smaller side because uh because what depend like on the smaller B boundary.
>> Mhm.
And uh yeah so like uh firstly like I initialize like left equals to zero and right equals to n minus one and um uh left max equals to also zero and uh right max also zero and then uh >> okay then you'll kind of converge so yeah sorry >> so you will kind of move from both ends >> towards the middle. Yeah.
>> Okay.
>> So, so first like I compare like left side is smaller than what depend on like left max and uh if like current height less than uh left max then like water stored.
>> Mhm.
>> Otherwise like uh updated left max.
>> Okay. Okay.
Got it. Uh I I would suggest you can start implementing. Okay. Then >> yeah.
>> Okay. Okay. Can I >> Sounds good to me. So you can start implementing.
>> Okay.
>> Okay.
Yes sir.
Hello.
>> Yes.
>> Uh yes sir. Done.
>> Okay. Uh can you uh walk me through the solution once?
>> Yeah sure. So there like uh I'm using two pointer. So like there firstly I initialize the variables like left equals to zero right equals to like array length minus one and left max= to right max what equals to zero. After that like I'm using while loop is here.
So like we are checking here like um we can say uh left less than right. So that time we are like checking the condition >> if a left one element is like uh small from like right one.
>> Mhm.
>> So that time like uh I am again checking one condition if like uh array left greater than equals to left max.
So in starting it is zero. So like that uh after that I'm assigning this value inside left max otherwise if condition is false then else >> okay >> like that yeah after that like uh I um I am doing like u firstly we can say yeah so like uh we can say like if current height is smaller >> so then water can be uh uh stored. So that's why like what are in this one condition this one I'm using like a this is a we can say like a formula like uh left max minus uh current height okay left okay [snorts] >> yeah so and uh same like uh for right side cases also like uh uh for right side cases like uh we are using also condition here like uh um array of right greater than of right max. So we are assigning this value inside right max.
>> In which option you're choosing to increment left and which option you are choosing to decrement right.
>> Uh so like uh it's depend on a condition.
>> Okay. Can you explain that condition to me?
>> For uh left like uh we are using this things.
>> Mhm.
>> Uh so like uh we can say like uh uh uh I use like a ar r left ar right or like less than less than equals to like both uh uh we can say uh both Mhm.
Yeah. So it is used for left and this one is used for right. We can say these conditions.
Okay.
Okay. And what is the space and time complexity for this solution?
So for this uh so time complexity like uh we are uh I'm like traversing uh the array only once so like there we are using because two pointers so their complexity time complexity is O of N for this >> okay >> and uh space complexity space complexity we can say like uh um I'm using like um uh sorry I'm I'm not using yeah so like uh I'm not using any extra data structure here so there space complexity only of one got it okay uh okay now I think I'm done with my questions Just say you have any question for
Related Videos
resume fixed instantly 😭 Comment “app”andI’ll sendyou the link #parakeetaipartnership #resumetips
Ritcareer
686 views•2026-05-31
3D Basics in C
HirschDaniel
2K views•2026-06-05
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
Making Minecraft Clone with C++ & Raylib
PecaCSLive
686 views•2026-06-04
Instagram accounts got PWNed
EricParker
13K views•2026-06-03
So What's Odin Lang Even Good For
TechOverTea
131 views•2026-06-01
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











