LeetCode Question · Jan 2024 · Washington DC

Please tell where I am Wrong , it Failed on private Test case

122 views

Question Details

The problem is an over simplification of the flow of liquid. - A terrain is given as a grid of cells of random elevations. The grid is always odd sized...

Full Details

The problem is an over simplification of the flow of liquid. - A terrain is given as a grid of cells of random elevations. The grid is always odd sized and is always a square. - A liquid is poured at the central cell. Water can flow only north-south or east-west; not diagnonally. - At the first step, the water level is the same as the central cell. - Water from one cell flows to a neighbouring cell if the level of water is equal to greater than the elevation of the neighbouring cell. - When the water flows to the neighbouring cell, the level of water is maintained. - If the water cannot flow to any new cell, the water level rises. - The simulation stops when the water reaches the end of the domain. - The output consists of the domain represented by . and W representing dry and wet terrain.

Below is an example

Input Format

7 494 88 89 770 984 726 507 340 959 220 301 639 280 290 666 906 632 824 127 505 787 673 499 843 172 193 613 154 544 211 124 60 575 572 389 635 170 174 946 593 314 300 620 167 931 780 416 954 275

Water level and location of water:


Current water level: 172

.......

.......

.......

...W...

...W...

.......

.......


Current water level: 172

.......

.......

.......

...W...

..WW...

.......

.......


Current water level: 172

.......

.......

.......

...W...

..WW...

.......

.......

Cannot flow, increasing water level to 173


Current water level: 173

.......

.......

.......

...W...

..WW...

.......

.......

Cannot flow, increasing water level to 174


Current water level: 174

.......

.......

.......

...W...

..WW...

..W....

.......


Current water level: 174

.......

.......

.......

...W...

..WW...

.WW....

.......


Current water level: 174

.......

.......

.......

...W...

..WW...

.WW....

.W.....


Current water level: 174 Reached edge, exiting.

Solution:

Output Format

.......

.......

.......

...W...

..WW...

.WW....

.W.....

Constraints: First line of the input has dimension n of (n X n) matrix. Followed by the matrix itself as shown below in the sample input

this is code , Tell me where i am Wrong

include

using namespace std;

void print(vector>&ans){
int n=ans.size();
char st[n][n];

for(int i=0 ;i<n ;i++){
for(int j=0 ;j<n ;j++){
if(ans[i][j]==-1)st[i][j]=\'W\';
else st[i][j]=\'.\';
}
}

for(int i=0 ;i<n ;i++){
for(int j=0 ;j<n ;j++){
cout<<st[i][j];
}
cout<<endl;
}

}

bool reachEnd;
void dfs(int r , int c ,vector>&arr , vector>&ans , int drow[] , int dcol[] , int &ini , int &tempr , int &tempc){
ans[r][c]=-1;
int n=arr.size();
if(r==0 || r==n-1 || c==0 || c==n-1){
reachEnd=true;
return;
}
tempr=r;
tempc=c;
for(int i=0 ;i<4 ; i++){
int nrow=r+drow[i];
int ncol=c+dcol[i];

if(nrow>=0 && nrow=0 && ncol<n && ans[nrow][ncol]!=-1 && arr[nrow][ncol]<ini){
dfs(nrow ,ncol ,arr, ans , drow ,dcol ,ini , tempr ,tempc);
}
}

}
void solve(vector>&arr , int sr , int sc ,int n){

int ini=arr[sr][sc];
vector>ans=arr;
ans[sr][sc]=-1;
int tempr=n/2,tempc=n/2;
int drow[4]={-1,0,+1,0};
int dcol[4]={0,+1 ,0,-1};
while(!reachEnd){
dfs(sr,sc,arr,ans , drow ,dcol ,ini , tempr ,tempc);
ini++;
sr=tempr;
sc=tempc;
}

if(reachEnd){
print(ans);
}

}

int main(){
int n;
cin>>n;

vector>arr(n,vector(n));
for(int i=0 ;i>arr[i][j];
}
}

solve(arr,n/2,n/2 ,n);

return 0;
}

Free preview — 6 questions shown. Unlock all Square/Block questions →

About This Question

This is a reported interview question from a square/block interview for a swe role (senior level) reported in 2024.

It covers the following topics: Graph, Matrix .

About Square/Block Interview Reports

This question was reported by a candidate who interviewed at Square/Block. LeakCode aggregates interview reports from 10+ sources, including 1Point3Acres, Glassdoor, LeetCode Discuss, Blind, Reddit, Indeed, and Nowcoder. Each report is translated where necessary, deduplicated against existing entries, and tagged by company, role, round type, and reporting date.

Use this question as one calibration data point, not a memorization target. Companies typically rotate their question pools every 2-4 months; the exact wording of a 2024 question may differ from what you encounter today. The underlying pattern, difficulty level, and follow-up depth at Square/Block are the higher-signal extractions to take from this report.

For broader preparation context, the Square/Block interview process typically includes a recruiter screen, one or two technical phone screens, and a 4-5 round on-site loop covering coding, system design (at L4+ levels), and behavioral. Reports tagged on LeakCode show the round-by-round distribution and typical difficulty calibration. To browse questions filtered by round type and seniority, use the company hub linked above.

How To Practice This Type of Question

Solve similar problems on LeetCode under timed conditions (25-35 minutes per medium difficulty). The goal is pattern recognition: recognize the underlying technique (sliding window, two-pointer, BFS, memoized recursion, etc.) within 60-90 seconds of reading. Strong candidates verbalize their hypothesis out loud before coding, then iterate based on feedback. Weak candidates dive into implementation immediately, lose time on the wrong approach, and run out of time for follow-ups.

Companies update their question pools every 2-4 months. The exact wording of any given question may have been retired by the time you interview. Focus your prep on the pattern, not the specific problem. The patterns that appear in Square/Block reports consistently are the ones worth investing in; one-off niche problems are not.

During Your Square/Block Round

Apply the standard interview round template: clarify requirements (2-3 minutes), state your approach out loud and confirm direction with the interviewer (3-5 minutes), code with narration (15-25 minutes), test with concrete examples including edge cases (5 minutes), discuss optimization or trade-offs if time permits (5 minutes). This template is universally accepted across FAANG and adjacent companies; deviating from it produces weaker interviewer feedback signal.

The single most predictive failure mode in Square/Block reports tagged "no hire": not asking clarifying questions. Interviewers are explicitly trained to weight this. Strong candidates ask 3-5 clarifying questions even on problems that look obvious; weak candidates dive into code immediately. The clarifying-question check is often the first signal recorded in the interviewer's written notes.