Solve three C++ algorithmic tasks
Company: Akuna Capital
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Solve three C++ algorithmic tasks states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Diminishing Prices Revenue (Maximize Sales Revenue)
Constraints
- 1 <= inventory.size() <= 2e5
- 1 <= orders <= 1e13 (must fit in a 64-bit integer)
- 1 <= inventory[i] <= 1e9
- Return the answer modulo 1_000_000_007
Examples
Input: ([2, 5], 4)
Expected Output: 14
Explanation: Sell 5,4,3,2 from the type starting at 5 -> 5+4+3+2 = 14.
Input: ([3, 5], 6)
Expected Output: 19
Explanation: Sell 5,4 then both types at 3 (3,3), then 2 -> 5+4+3+3+2 = ... greedy max = 19.
Input: ([2, 8, 4, 10, 6], 20)
Expected Output: 110
Explanation: Classic LC1648 example; greedy top-down sales total 110.
Input: ([1000000000], 1)
Expected Output: 1000000000
Explanation: Single sale at the max price; no modulo wrap needed.
Input: ([1], 1)
Expected Output: 1
Explanation: One unit at price 1.
Input: ([2, 2, 2], 3)
Expected Output: 6
Explanation: Three types each at 2; sell one unit from each -> 2+2+2 = 6.
Hints
- Always sell the most expensive unit available; sort prices descending and peel off the top band.
- When `width` types all sit above the next distinct price level, the prices you collect form an arithmetic series — sum it in O(1) with n*(top+bot)/2 instead of looping unit by unit.
- Watch the partial last band: full_rows = remaining // width complete rows, then `remaining % width` extra columns each sell one more unit at price (cur - full_rows).
- Take the modulo only at the end of each block sum; `orders` up to 1e13 needs a 64-bit type.
Count Subarrays With Fixed Bounds
Constraints
- 1 <= nums.size() <= 2e5
- -1e9 <= nums[i] <= 1e9
- minVal <= maxVal
- The count can exceed 32-bit range; use a 64-bit accumulator
Examples
Input: ([1, 3, 5, 2, 7, 5], 1, 5)
Expected Output: 2
Explanation: Valid subarrays are [1,3,5] and [1,3,5,2] (each has min 1 and max 5).
Input: ([1, 1, 1, 1], 1, 1)
Expected Output: 10
Explanation: Every one of the 4*5/2 = 10 subarrays has min == max == 1.
Input: ([1, 2, 3], 5, 5)
Expected Output: 0
Explanation: 5 never appears, so no subarray qualifies.
Input: ([5], 5, 5)
Expected Output: 1
Explanation: The single-element subarray [5] has min == max == 5.
Input: ([2, 1, 4, 3], 1, 4)
Expected Output: 4
Explanation: [2,1,4], [1,4], [1,4,3], [2,1,4,3] all have min 1 and max 4.
Input: ([-1000000000, 0, 1000000000], -1000000000, 1000000000)
Expected Output: 1
Explanation: Only the full array spans both extreme bounds.
Hints
- Any value outside [minVal, maxVal] can never be part of a valid subarray — treat its index as a fresh left boundary and reset state.
- For a subarray to qualify, it must contain at least one minVal and at least one maxVal; track the latest index of each.
- Subarrays ending at i are valid only if they start after the boundary and include both the latest minVal and latest maxVal, so the count is min(lastMin, lastMax) - leftBound.
- When minVal == maxVal, the same element satisfies both conditions simultaneously.
Binary Matrix Row Equivalence Under Column Flips
Constraints
- 1 <= n <= 1e5
- 1 <= m <= 30 (a row fits in a 32-bit mask)
- Every entry of A is 0 or 1
- Return a 64-bit integer (up to ~n^2/2 pairs)
Examples
Input: ([[0, 1, 0], [1, 0, 1], [0, 0, 0]],)
Expected Output: 1
Explanation: Rows [0,1,0] and [1,0,1] are complements (equivalent); [0,0,0] is alone. One pair.
Input: ([[0, 0], [0, 0], [0, 0]],)
Expected Output: 3
Explanation: All three rows are identical: 3*2/2 = 3 pairs.
Input: ([[1, 0], [0, 1], [1, 1], [0, 0]],)
Expected Output: 2
Explanation: [1,0]~[0,1] (complements) and [1,1]~[0,0] (complements): 1 + 1 = 2 pairs.
Input: ([[1, 1, 0]],)
Expected Output: 0
Explanation: A single row forms no pairs.
Input: ([[0, 1], [1, 0], [0, 1], [1, 0]],)
Expected Output: 6
Explanation: All four rows collapse to one equivalence class: 4*3/2 = 6 pairs.
Input: ([],)
Expected Output: 0
Explanation: Empty matrix has no rows and no pairs.
Hints
- Flipping a column XORs the same bit across all rows; two rows are reachable from each other only if they are identical or exact complements.
- Canonicalize each row by XOR-ing it with its own first bit, forcing the leading bit to 0 — identical and complementary rows then map to the same key.
- Pack the m <= 30 bits into a single integer mask so each row's canonical form is one hashable key.
- Within each equivalence group of size c, the number of unordered pairs is c*(c-1)/2; sum across groups using a 64-bit accumulator.