Solve four algorithmic problems
Company: Citadel
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This multi-part question evaluates algorithmic problem-solving skills across combinatorics and subsequence counting, array partitioning with prefix-sum or dynamic programming optimization, constrained subset selection and feasibility reasoning, and reachable-state/MEX analysis under bounded operations, alongside complexity analysis and efficient algorithm design. It is commonly asked in Coding & Algorithms interviews because it probes reasoning about constructive sequence operations, optimization under constraints, and achievable-state characterization while balancing time and space complexity, and it tests both conceptual understanding and practical application.
Part 1: Count Extendable Prefixes for '10' Subsequences
Constraints
- 0 <= len(s) <= 200000
- s contains only characters '0' and '1'
- 0 <= k <= 10^18
Examples
Input: ('', 0)
Expected Output: 0
Explanation: There are no non-empty prefixes.
Input: ('1010', 2)
Expected Output: 2
Explanation: Prefixes '1' and '10' can be extended to have exactly 2 "10" subsequences.
Input: ('000', 5)
Expected Output: 3
Explanation: Every prefix has zero ones, so we can append 5 ones followed by a zero to create exactly 5 subsequences.
Input: ('111', 2)
Expected Output: 2
Explanation: Prefixes with 1 or 2 ones can reach k = 2; the prefix with 3 ones cannot add a positive amount less than 3.
Input: ('10', 0)
Expected Output: 1
Explanation: Prefix '1' already has 0 "10" subsequences, but prefix '10' already has 1.
Hints
- Track, for each prefix, the current number of '1' characters and the current number of "10" subsequences.
- Appending a '0' adds exactly the current number of ones to the "10" count; appending '1's first lets you choose any larger contribution.
Part 2: Maximize Alternating Four-Segment Sum
Constraints
- 0 <= len(a) <= 3000
- -10^9 <= a[i] <= 10^9
- Segments may be empty
Examples
Input: ([],)
Expected Output: 0
Explanation: The only split has four empty segments.
Input: ([-7],)
Expected Output: 7
Explanation: Place the single element in a negative-sign segment, giving -(-7) = 7.
Input: ([1, -2, 3, -4],)
Expected Output: 10
Explanation: Use segments [1], [-2], [3], [-4], giving 1 - (-2) + 3 - (-4) = 10.
Input: ([1, 2, 3],)
Expected Output: 6
Explanation: Put the whole array in a positive-sign segment.
Input: ([-5, -1],)
Expected Output: 6
Explanation: Put the whole array in a negative-sign segment, giving -(-6) = 6.
Hints
- Rewrite the expression using prefix sums and the three boundary positions.
- If P[x] is a prefix sum, the value can be written as 2 * (P[i1] - P[i2] + P[i3]) - total_sum.
Part 3: Select the Largest Agreeable Developer Team
Constraints
- 0 <= n <= 200000
- len(lowerSkill) == len(higherSkill) == n
- 0 <= lowerSkill[i] <= n
- 0 <= higherSkill[i] <= n
- Developer skills are ordered by array index: developer i has skill i + 1
Examples
Input: ([], [])
Expected Output: 0
Explanation: There are no developers.
Input: ([0], [0])
Expected Output: 1
Explanation: A single developer has no lower- or higher-skilled teammates.
Input: ([0, 1, 2, 3, 4], [4, 3, 2, 1, 0])
Expected Output: 5
Explanation: All developers can be chosen.
Input: ([0, 0, 0], [0, 0, 0])
Expected Output: 1
Explanation: Any team of size 2 would give someone a lower- or higher-skilled teammate, which is not allowed.
Input: ([0, 0, 2, 2], [2, 2, 0, 0])
Expected Output: 2
Explanation: A valid team of size 2 exists, but no valid team of size 3 exists.
Hints
- For a proposed team size m, the j-th selected developer in increasing skill order has exactly j lower-skilled teammates and m - 1 - j higher-skilled teammates.
- Check whether size m is feasible greedily from low skill to high skill, then binary search the answer.
Part 4: Find All Achievable MEX Values with Bounded Increments
Constraints
- 0 <= n <= 200000
- If n > 0, 0 <= memoryBlocks[i] <= n - 1
- Operations may be performed any number of times, including zero
Examples
Input: ([],)
Expected Output: [0]
Explanation: The empty array has MEX 0.
Input: ([0],)
Expected Output: [1]
Explanation: The only value is already 0 and cannot be increased, so the only MEX is 1.
Input: ([0, 1, 2],)
Expected Output: [0, 1, 3]
Explanation: MEX 2 is impossible because the value 2 is stuck, but 0, 1, and 3 are achievable.
Input: ([1, 1, 1],)
Expected Output: [0]
Explanation: Value 0 can never be created, so only MEX 0 is achievable.
Input: ([0, 2, 2, 3],)
Expected Output: [0, 1]
Explanation: We can achieve MEX 0 by increasing the 0, or MEX 1 by leaving 0 present. Value 1 cannot be created.
Hints
- To achieve MEX m, every value 0 through m - 1 must be constructible from distinct elements by only increasing values.
- The only value that cannot be moved upward is n - 1, so it creates a special obstruction when trying to make MEX n - 1.