Largest Submatrix With Rearrangements
A medium-tier problem at 75% community acceptance, tagged with Array, Greedy, Sorting. Reported in interviews at Directi and 1 others.
You're staring at a matrix problem that sounds straightforward until you realize the catch: you can rearrange rows. Directi and Samsung have both asked this one, and it trips up candidates who brute-force a solution without seeing the greedy angle. The trick isn't complex, but it's easy to miss in a live assessment. If this problem hits your OA and you blank on the pattern, StealthCoder solves it in seconds, invisible to the proctor. With a 75% acceptance rate, plenty of people get there, but the ones who understand the rearrangement leverage usually finish faster and cleaner.
Companies that ask "Largest Submatrix With Rearrangements"
Largest Submatrix With Rearrangements is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.
Get StealthCoderThe core insight is that you can reorder row elements freely, so the problem becomes: find the largest rectangle where all cells contain 1s, given that you can shuffle each row independently. The trick is to think in terms of column compression and greedy selection. For each possible height, you want to pick columns greedily such that all rows in your range have 1s in those columns. Sort the column counts by frequency of 1s and pick the widest contiguous block. Many candidates try to fix rows first or use dynamic programming when the answer is simpler: precompute 1-runs per row, then for each height k, count how many rows have at least k consecutive 1s, and find the maximum product. StealthCoder handles the greedy ordering and avoids the false starts.
Pattern tags
You know the problem.
Make sure you actually pass it.
Largest Submatrix With Rearrangements recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Largest Submatrix With Rearrangements interview FAQ
What's the actual trick to solving this?+
You can rearrange rows, so focus on: for each possible rectangle height, how many columns can you guarantee are all 1s? Sort columns by density of 1s and greedily pick the widest block. The trick is realizing rearrangement unlocks a simpler greedy state.
Why does the obvious DP approach fail here?+
Treating it like a standard largest rectangle in histogram problem ignores the rearrangement freedom. You'll waste time tracking column positions when you should be counting frequency of 1s across rows. The rearrangement constraint resets your DP intuition.
Is this still asked at Samsung and Directi?+
Both companies have asked it according to candidate reports. At 75% acceptance, it's treated as a medium-difficulty screening problem, not a final-round blocker. The rearrangement angle makes it memorable enough to stick in their rotation.
How does sorting relate to the solution?+
Sorting is essential: once you know the count of 1s in each row, you sort rows by their 1-run counts. Then you iterate through possible heights and greedily pick the widest submatrix. Sorting removes dependency on row order.
What's a common pitfall during the OA?+
Trying to track which rows and columns contribute to the answer, instead of just counting. You get bogged down in state tracking. The clean approach: for each height h, count how many rows have h consecutive 1s, then compute area. Focus on counts, not positions.
Want the actual problem statement? View "Largest Submatrix With Rearrangements" on LeetCode →