Code Room
CodingMediumcod-g1449
Subject GreedyLevel Entry–Mid~14 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

You are given a non-empty integer array and a number k >= 1. You must perform exactly k operations; each operation picks one index and flips the sign of that element, and the same index may be picked any number of times. Return the largest possible array sum after the k operations. Example: nums [3, -1, 0, 2] with k = 3 gives 6 — flip -1 once, then burn the remaining two flips on the 0.

Implement
max_sum_after_flips(nums: list[int], k: int) → int
Examples
in[[3,-1,0,2],3]out6
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.