Trapping Rain Water II Problem Statement

Given an M * N matrix where each cell's value represents its height in a 2-D elevation map, calculate the total volume of water that can be trapped after rainfall.

Input:

The first line contains an integer ‘T’ representing the number of test cases.
For each test case:
- The first line contains two integers 'M' and 'N', indicating the number of rows and columns respectively.
- The following 'M' lines, each containing 'N' space-separated integers, denote the height of each cell in the matrix.

Output:

For each test case, output a single integer representing the volume of water that can be trapped.
Print each result on a new line.

Example:

Input:
1
3 3
5 5 5
5 2 3
6 9 8
Output:
1
Explanation:

The given matrix forms a 2-D elevation map where, after rain, water is trapped leading to a total volume of 1 unit.

Constraints:

  • 1 <= T <= 10
  • 1 <= M,N <= 100
  • 1 <= matrix[i][j] <= 10^3

Note: You aren’t required to handle the printing of results; this is managed elsewhere. Your task is to implement the function to determine the trapped volume.

AnswerBot
4mo

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

  • Iterate through each cell in the matrix and calculate the trapped water based on the surrounding heights.

  • U...read more

Help your peers!
Select
Add answer anonymously...

MindTickle Software Developer interview questions & answers

A Software Developer was asked 6mo agoQ. Design the Low-Level Design (LLD) for a Parking Lot.
A Software Developer was asked Q. Ninja and His Meetings Problem Statement Ninja has started a new startup with a ...read more
A Software Developer was asked Q. Smallest Subarray With K Distinct Elements Given an array A consisting of N inte...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Ninja and His Meetings Problem Statement Ninja has started a new startup with a ...read more
A Software Developer was asked Q2. Smallest Subarray With K Distinct Elements Given an array A consisting of N inte...read more
A Software Developer was asked Q3. Trapping Rain Water II Problem Statement Given an M * N matrix where each cell's...read more
MindTickle 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