DSA interview question
You're building a resource-allocation optimizer for a construction company. There's a row of n plots of land along a highway, each with an estimated profit if developed. Due to zoning regulations, you cannot develop two adjacent plots (to maintain required setback distances between construction sites). Your task: given the profit values for each plot, find the maximum total profit achievable by selecting a subset of plots such that no two selected plots are adjacent.
Example: profits [2, 1, 4, 9] → select plots 0 and 2, or plot 1 and 3, but selecting plot 2 and 3 alone (9+... no, they're adjacent) — best is 2 + 9 = 11 (plots 0 and 3 are not optimal either; verify: 1+9=10, 2+9=11, 2+4=6) → answer is 11.
int maximumNonAdjacentSum(vector<int>& nums);
Input nums | Output | Explanation |
|---|---|---|
[2,1,4,9] | 11 | Select plots 0 and 3 → 2+9=11 |
[1,2,4] | 5 | Select plots 0 and 2 → 1+4=5 |
[] | 0 | No plots, nothing to select |
[5] | 5 | Single plot, select it |
[3,7] | 7 | Two plots, select the bigger one |
[5,5,10,100,10,5] | 110 | Select plots 0/1 (5), plot 3 (100), plot 5 (5) → best combo = 5+100+5=110 |
[1,2,3,1,5] | 9 | Select plots 0,2,4 → 1+3+5=9 |
(Confirm: all values are positive per problem statement, so this differs slightly from House Robber where 0/negative edge cases might need explicit handling — here selecting nothing is never beneficial.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in