42. Trapping Rain Water

42. Trapping Rain Water


➡️ Interviewer: Great job Sideon, you have implemented an optimal solution for the problem "Trapping Rain Water". Can you explain your thought process on how you arrived at this solution?

👨‍🦰 Sideon: Thank you! Initially, I started with the brute force approach where I was checking for each element of the array and calculating the trapped water between the left and right boundaries. Then, I optimized it by precomputing the maximum element to the left and right for each element in the array, which helped to calculate the trapped water easily. Finally, I used the two pointer approach to further optimize the solution and arrive at the current solution.

➡️ Interviewer: That's a great thought process, and it is good to see that you started with the brute force approach and optimized it to the current solution.

Can you explain your solution and how it works, and also explain the time and space complexity of your solution?

👨‍🦰 Sideon: Sure. In this solution, I am using two pointers to traverse the array from both ends. I have two variables to store the maximum height from the left and right side. Then, I check for the current element at the left pointer and right pointer. If the height of the left element is less than the height of the right element, then I check if the left element's height is greater than the left maximum height. If yes, then I update the left maximum height. Otherwise, I calculate the trapped water by subtracting the height of the left element from the left maximum height and add it to the total trapped water. Similarly, if the height of the right element is less than the height of the left element, then I check if the right element's height is greater than the right maximum height. If yes, then I update the right maximum height. Otherwise, I calculate the trapped water by subtracting the height of the right element from the right maximum height and add it to the total trapped water. I repeat this process until the left and right pointers cross each other.

The time complexity of this solution is O(n) as we are traversing the array only once. The space complexity is O(1) as we are not using any extra space apart from the given input array.

➡️ Interviewer: That's a great solution, Sideon. You have covered all the edge cases and optimized the solution in the best possible way.

Can you explain your variable naming and any test cases that you might have considered while implementing this solution?

👨‍🦰 Sideon: Sure. I have used intuitive variable names like "left" and "right" for the two pointers and "leftMax" and "rightMax" for the maximum heights from the left and right sides. For test cases, I considered arrays with different heights, arrays with no trapped water, arrays with only one element, arrays with all elements having the same height, and arrays with the maximum and minimum values.

➡️ Interviewer: Alright, now that we have talked about the optimal solution, can you please write the code for me?

👨‍🦰 Sideon: Sure, I'll write the code in Java. (Starts writing code)

public int trap(int[] height) {
    int left = 0, right = height.length - 1;
    int leftMax = 0, rightMax = 0;
    int water = 0;
    while (left < right) {
        if (height[left] < height[right]) {
            if (height[left] >= leftMax) {
                leftMax = height[left];
            } else {
                water += leftMax - height[left];
            }
            left++;
        } else {
            if (height[right] >= rightMax) {
                rightMax = height[right];
            } else {
                water += rightMax - height[right];
            }
            right--;
        }
    }
    return water;
}

➡️ Interviewer: Take your time and let me know if you need any help. Can you please explain to me what you're doing in each step?

👨‍🦰 Sideon: Sure, so first I'm initializing two pointers at the start and end of the given array. Then I'm using two variables, maxLeft and maxRight, to keep track of the maximum height from the left and right pointers respectively.

➡️ Interviewer: Okay, that makes sense. And what's the purpose of maxLeft and maxRight?

👨‍🦰 Sideon: The purpose of maxLeft and maxRight is to keep track of the maximum height of the left and right walls that can trap water. We need to check the height of the wall at each index and find the maximum height from the left and right of that index.

➡️ Interviewer: Great! And what's the significance of the while loop?

👨‍🦰 Sideon: The while loop is used to traverse the array and check the maximum height from both sides. We are comparing the height of the wall at each index with the maximum height from left and right sides and taking the minimum of the two. We are then subtracting the height of the wall at that index from the minimum value we found, which gives us the amount of water that can be trapped at that index.

➡️ Interviewer: Alright, that sounds good. Let me take a look at your code. (Looks at the code and runs some test cases) Your code looks correct and all the test cases are passing. Great job!

👨‍🦰 Sideon: Thank you, I appreciate the feedback.

➡️ Interviewer: Just one thing I would like to mention is that you could use a single loop instead of two while loops to traverse the array and find the maximum height from left and right.

👨‍🦰 Sideon: Ah, I see. Yes, that would make the code more efficient.

➡️ Interviewer: Exactly. And also, you could use better variable names for improved readability. For example, instead of using 'l' and 'r' for left and right pointers, you could use 'leftPtr' and 'rightPtr'. That way, it will be easier for others to understand your code.

👨‍🦰 Sideon: Thank you for the suggestion. I'll make sure to keep that in mind in the future.

➡️ Interviewer: Great, glad to hear that. Overall, your approach was good and your code is efficient and correct. It was a pleasure talking to you today, Sideon.

👨‍🦰 Sideon: Thank you so much, it was a great experience for me as well.

Time to fly, Keep an eye out for similar Leetcode Interview Simulations on various problems, Though we kicked off with an easy one today, always check the comments.

Thanks for the support @Fahad Ahmad & @Jennie D Souza 💖 for both.