Trapping Rain Water in 2-D Elevation Map

You're given an M * N matrix where each value represents the height of that cell on a 2-D elevation map. Your task is to determine the total volume of water that can be trapped after it rains.

Example:

Input:
matrix = [[5, 5, 5],
[5, 2, 3],
[6, 9, 8]]
Output:
1
Explanation:

The elevation map allows 1 unit volume of water to be trapped, as illustrated in the image.

Constraints:

  • 1 <= T <= 10
  • 1 <= M, N <= 100
  • 1 <= matrix[i][j] <= 10^3
  • Where ‘T’ is the number of test cases, ‘M’ and ‘N’ are the dimensions of the given matrix.
  • Matrix[i][j] is the height of the cell at the ‘i-th’ row and ‘j-th’ column.
  • Time Limit: 1 sec.
Note:
You don't need to print anything, as it has already been taken care of. Just implement the given function.
AnswerBot
4mo

Calculate the total volume of water that can be trapped in a 2-D elevation map after rain.

  • Iterate over the matrix and find the maximum height on the borders.

  • Calculate the water trapped at each cell by...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at ION Group

Q. Explain the time complexity of a binary tree.
Q. What are the four pillars of OOP?
Q. Implement a stack using a linked list.
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits