Largest Square of Ones
Reported by candidates from FactSet's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You're facing the Largest Square of Ones problem at FactSet in September 2024, and you're on a timer. This is a classic dynamic programming grid problem that looks simple on the surface but punishes brute force. The trick is building a table that tracks the largest square ending at each cell, not scanning every possible square. If you blank on the DP transition, StealthCoder will feed you the pattern so you don't panic and time out.
Pattern and pitfall
The problem asks for the side length of the largest square containing only ones in a binary matrix. Most candidates try nested loops checking squares, which is O(n^3) or worse. The real solution uses a 2D DP table where dp[i][j] represents the side length of the largest square with bottom-right corner at (i, j). If the cell is 0, dp[i][j] is 0. If it's 1, dp[i][j] is the minimum of dp[i-1][j], dp[i][j-1], and dp[i-1][j-1], plus one. This runs in O(n*m) time. FactSet likely tests whether you recognize this pattern quickly and implement it cleanly. StealthCoder becomes your safety net if the recurrence doesn't snap into focus during the live OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Largest Square of Ones cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as maximal square. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass FactSet's OA.
FactSet reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Largest Square of Ones FAQ
Is this problem asking for the area or the side length?+
The problem asks for the largest square, which typically means side length. Check the expected output in the examples. If they want area, multiply your answer by itself. FactSet's phrasing usually specifies, but if you're unsure in the OA, output side length first.
Can I solve this with a greedy or brute-force approach?+
Greedy won't work. Brute force (checking all possible squares) is O(n^3) or worse and will fail on large inputs. DP is the only competitive solution. Once you see dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1, everything clicks.
What's the trick to avoid off-by-one errors?+
Index carefully. Start your DP loop at (1, 1), not (0, 0), since you need to check three neighbors. Handle the first row and column separately: they can only form squares of size 1. Many candidates skip this and get wrong answers.
How hard is this compared to other FactSet OA problems?+
Medium difficulty for FactSet. It's not tricky in implementation, but it's easy to overlook the DP insight and try brute force instead. If you recognize the pattern, it's 10-15 minutes. If you don't, you'll spend 30+ minutes backtracking.
Should I optimize space if I solve it in DP?+
Standard DP uses O(n*m) space. You can reduce to O(m) by keeping only the current and previous rows, but FactSet rarely requires this unless constraints are huge. Focus on correctness and clarity first. Optimize only if you have time and memory is explicitly tight.