DSA interview question
You're building a fitness-gamification app where a ninja trains for n days. Each day, the ninja can perform exactly one of three activities — running, fighting practice, or swimming — and earns a certain number of merit points based on which activity they choose that day. The catch: the ninja cannot perform the same activity on two consecutive days (to avoid overtraining one muscle group). Your task: given the merit points for each activity on each day, find the maximum total merit points the ninja can earn over all n days.
Example: for 3 days with points Day 0: [10, 40, 70] Day 1: [20, 50, 80] Day 2: [30, 60, 90] Best strategy: Day 0 → activity 2 (70), Day 1 → activity 1 (50), Day 2 → activity 2 (90) = 210.
int ninjaTraining(int n, vector<vector<int>>& points);
// points[day][activity], activity in {0, 1, 2}
Input points | n | Output | Explanation |
|---|---|---|---|
[[10,40,70],[20,50,80],[30,60,90]] | 3 | 210 | Day0→2(70), Day1→1(50), Day2→2(90)... wait must alternate, verify with code |
[[1,2,5],[3,1,1],[3,3,3]] | 3 | 11 | Day0→2(5), Day1→0(3), Day2→any except 0, best is 3 |
[[10,50,1]] | 1 | 50 | Single day, just pick the max |
[[1,1,1],[1,1,1]] | 2 | 2 | Forced to alternate, all equal, sum = 2 |
[[100,0,0],[0,100,0],[0,0,100]] | 3 | 300 | Perfectly alternating best activities each day |
(Confirm: exactly 3 activities per day; the very first day has no "previous day" restriction.)
k activities? (Affects whether I hardcode 3 or write a generalized loop.)n be 0 (edge case to handle)?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