Longest Path
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Microsoft's August OA included a longest path problem that caught candidates off guard. You're not looking for a simple greedy walk. The trick is recognizing this as a tree or DAG traversal that requires memoization or DP to avoid recalculating the same subproblems. If you blank on the exact formulation during the live OA, StealthCoder will read the problem and hand you the pattern in seconds, so you can focus on the code instead of the concept.
Pattern and pitfall
Longest path problems typically hide a dynamic programming structure. On a tree, you're tracking the longest path passing through or starting from each node. On a DAG, you're computing longest paths in topological order. The common pitfall is brute-force DFS without memoization, which will time out. The candidate who recognizes early that you need to store results (either via recursion with memoization or bottom-up DP) wins. Microsoft tests whether you spot the DP opportunity before you code. StealthCoder lets you confirm the input shape (tree vs graph) and the constraints in real time, then gives you the state definition you need to structure the solution.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Longest Path 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Path FAQ
Is this a tree or a general graph?+
Without the full problem text, assume you'll need to handle both. If it's a tree, DFS with memoization is cleaner. If it's a DAG, topological sort followed by DP is safer. The input constraints will make it clear. Read the problem carefully during the OA.
What's the DP state?+
Usually dp[node] = longest path starting or ending at that node. You may need to track both directions (longest path down, longest path up) if you're computing the global maximum. Start with the simplest state and expand if needed.
Will brute force pass?+
Almost never. If the graph has even 1000 nodes and you're recomputing the same subproblems, you'll hit the time limit. Memoization is non-negotiable. Cache everything.
How do I prepare in 24 hours?+
Review longest increasing subsequence and tree diameter problems on LeetCode. Understand the difference between top-down memoization and bottom-up DP. Sketch out a small example by hand to nail the state transition before coding.
Is this still asked at Microsoft?+
Yes. August 2024 confirms it. Microsoft loves graph and tree DP problems in their OAs. Expect variants on path problems, subtree problems, and connectivity. Know your memoization patterns cold.