cswork / libraryOpen practice workspace

Coding questions · Kneron

Compute sum of minima with removals

Compute sum of minima with removals evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

You are given an array weights[0..n-1] of positive integers representing product weights in a line. Repeat until no products remain: choose the lightest remaining product (if multiple, choose the one with the smallest current index), add its weight to a running total, and remove it along with up to its two current neighbors—one on the left and one on the right if they still exist. If only one neighbor exists, remove just that neighbor. Return the final total. Example: weights = [4, 3, 2, 1] -> choose 1 (remove 2 and 1), remaining [4, 3]; choose 3 (remove 3 and 4); total = 1 + 3 = 4. Implement a function that returns this total for any input array.

What a Strong Answer Covers

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?

Compute Sum of Minima with Neighbor Removals

You are given an array weights[0..n-1] of positive integers representing product weights arranged in a line. Repeat the following until no products remain:

  1. Choose the lightest remaining product. If several products share the minimum weight, choose the one with the smallest current index.
  2. Add its weight to a running total.
  3. Remove it along with up to its two current neighbors — the one immediately to its left and the one immediately to its right, if they still exist. If only one neighbor exists, remove just that neighbor.

Return the final running total.

Note that "current" neighbors are determined by the remaining products at the moment of selection: when products are removed, the line closes up so that previously non-adjacent products can become neighbors.

Example: weights = [4, 3, 2, 1]. Choose 1 (the minimum); remove 1 and its left neighbor 2, leaving [4, 3]. Choose 3 (now the minimum); remove 3 and its left neighbor 4. Total = 1 + 3 = 4.

Constraints

  • 1 <= n (the array may also be empty, in which case return 0)
  • All weights are positive integers
  • On ties for the minimum, the smallest current index is chosen
  • Neighbors are determined by the current (post-removal) arrangement, not the original indices

Hints

  1. Maintain a doubly-linked-list view of the line using left[] and right[] index arrays so that after a removal the surviving products close up and become neighbors in O(1).
  2. Each round, scan for the lightest non-removed product, preferring the smallest index on ties; add its weight, then remove it plus its current left and right neighbors if they exist.
  3. Only the chosen (minimum) product's weight is added to the total — the removed neighbors do NOT contribute to the sum.

Solution

Solution Alignment

The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.

Problem Restatement

You are given an array weights[0..n-1] of positive integers representing product weights in a line. Repeat until no products remain: choose the lightest remaining product (if multiple, choose the one with the smallest current index), add its weight to a running total, and remove it along with up to its two current neighbors—one on the left and one on the right if they still exist. If only one neighbor exists, remove just that neighbor. Return the final total. Example: weights = [4, 3, 2, 1] -> choose 1 (remove 2 and 1), remaining [4, 3]; choose 3 (remove 3 and 4); total = 1 + 3 = 4. Implement a function that returns this total for any input array.

Recommended Approach

Start with a brute-force baseline to confirm correctness, then identify the repeated work or ordering property that enables a better data structure such as a hash map, heap, stack, queue, two pointers, prefix sums, BFS/DFS, or dynamic programming. Write the implementation around a small invariant and test that invariant directly.

Correctness

The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.

Complexity

State the baseline complexity and the optimized complexity. For most interview constraints, justify why the optimized approach meets the expected input size.

Edge Cases and Tests

Empty and singleton inputs, duplicates, ties, invalid inputs, boundary values, and tests that exercise the main invariant.