Tip 1: Practice coding questions daily on coding platforms.
Tip 2: Prepare for system design.
Tip 1: Don’t include false information on your resume.
Tip 2: Be thoroughly familiar with the work you’ve mentioned on your resume.


The width of each bar is the same and is equal to 1.
Input: ‘n’ = 6, ‘arr’ = [3, 0, 0, 2, 0, 4].
Output: 10
Explanation: Refer to the image for better comprehension:

You don't need to print anything. It has already been taken care of. Just implement the given function.
Step 1: Initially, I approached the problem with a brute-force solution where I calculated the trapped water for each bar by comparing the maximum heights of bars to the left and right of the current bar. While this approach worked, it was inefficient with a time complexity of O(n^2), and I realized it wouldn't scale well for larger inputs.
Step 2: The interviewer pointed out that while my solution was correct, it was not optimized. I was asked to improve the time complexity. I then thought of optimizing the solution using extra space to store the maximum heights on the left and right in two separate arrays.
Step 3: After some thinking, I applied a two-pointer approach that allowed me to solve the problem with a time complexity of O(n) and O(1) extra space. This method efficiently calculates the trapped water by maintaining two pointers (left and right), iterating over the elevation map, and calculating trapped water based on the minimum of the maximum heights encountered from both directions. The interviewer appreciated this optimized solution, which was both time and space-efficient.



Each product can cross the integer limits, so we should take modulo of the operation.
Take MOD = 10^9 + 7 to always stay in the limits.
Can you try solving the problem in O(1) space?
Step 1: Initially, I thought about using the division method, where I would calculate the total product of the array and divide it by each element to get the result for each index. However, since division was not allowed in the problem constraints, I needed to find another approach.
Step 2: The interviewer asked me to rethink my approach to solve the problem in O(n) time without division. I realized I could solve the problem by creating two auxiliary arrays to store the prefix and suffix products of each element.
Step 3: I implemented a solution where I first created a prefix product array, where each element at index i holds the product of all the numbers before i. Then, I created a suffix product array, where each element at index i holds the product of all the numbers after i. By multiplying the prefix and suffix arrays element-wise, I got the desired result. This solution ran in O(n) time but used extra space.
Step 4: To further optimize the space complexity, I used a single array for the result and updated it in two passes: first for the prefix product and then iterating from the end to incorporate the suffix product. This allowed me to achieve O(n) time complexity with O(1) space for the result array, which met the problem’s constraints. The interviewer was happy with this optimized approach.
Design a URL shortening service like bit.ly.

Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
What is recursion?