Solve odd-string, digit swap, patient slot assignment
Company: DRW
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This multi-part problem evaluates string manipulation and parity/combinatorics for constructing odd-occurrence strings, greedy optimization and minimal-change reasoning for digit-swap distance minimization, and bipartite matching/assignment skills for patient slot allocation.
Odd-Occurrence String
Constraints
- 1 ≤ N ≤ 200000
- The returned string must have length exactly N.
- Every lowercase letter that appears in the result must appear an odd number of times.
- Letters that do not appear are unconstrained.
Examples
Input: (1,)
Expected Output: 'a'
Explanation: N is odd, so a single 'a' (count 1, odd) is returned.
Input: (2,)
Expected Output: 'ab'
Explanation: N is even: 'a' once and 'b' once — both counts odd.
Input: (3,)
Expected Output: 'aaa'
Explanation: N is odd: 'a' three times (count 3, odd).
Input: (6,)
Expected Output: 'aaaaab'
Explanation: N is even: 'a' five times (odd) and 'b' once (odd).
Input: (7,)
Expected Output: 'aaaaaaa'
Explanation: N is odd: 'a' seven times (odd).
Hints
- If N is odd, a single repeated letter already satisfies the condition.
- If N is even, you cannot use a single letter (it would appear an even number of times). Split off exactly one occurrence of a second letter so both counts become odd.
- N-1 is odd whenever N is even, and 1 is odd, so 'a'*(N-1) + 'b' works for even N.
Minimum Position-Wise Swaps to Close Two Numbers
Constraints
- 1 ≤ N ≤ 100000
- len(S) == len(T) == N
- S and T consist of digits '0'..'9' with no leading zeros.
- Only same-position S[i] ↔ T[i] swaps are allowed; each such swap costs 1.
Examples
Input: ('12', '12')
Expected Output: 0
Explanation: S and T are identical; difference is already 0, so no swaps are needed.
Input: ('19', '91')
Expected Output: 0
Explanation: Keeping both positions gives |19-91|=72; any single swap gives the same magnitude, so 0 swaps already minimizes.
Input: ('3', '8')
Expected Output: 0
Explanation: |3-8|=5 and swapping gives |8-3|=5 — same magnitude, so the minimum-swap minimizer uses 0 swaps.
Input: ('99', '11')
Expected Output: 1
Explanation: Keeping gives |99-11|=88; swapping the tens position once yields |19-91|=72, the smallest achievable, using 1 swap.
Input: ('73511', '77975')
Expected Output: 1
Explanation: Swapping only the most significant differing position drives the difference to its minimum 3536 with a single swap.
Input: ('532', '900')
Expected Output: 0
Explanation: Keeping all positions already yields the minimum achievable |difference|, so 0 swaps.
Hints
- Swapping position i flips the sign of that position's contribution (S[i]−T[i])·10^(N−1−i) to int(S)−int(T).
- First make the absolute difference as small as possible; only then minimize the number of sign flips (swaps).
- If S and T are already equal, the difference is 0 and zero swaps are needed.
Patient Slot Assignment
Constraints
- 1 ≤ N ≤ 100000 (N = len(A) = len(B))
- 1 ≤ S ≤ 100000
- 1 ≤ A[i], B[i] ≤ S for every patient i
- A[i] may equal B[i].
Examples
Input: ([1, 2], [2, 3], 3)
Expected Output: True
Explanation: Two patients, edges 1-2 and 2-3; the component has 2 edges and 3 nodes, so assignment is possible (e.g. patient 0 → slot 1, patient 1 → slot 3).
Input: ([1, 1], [2, 2], 2)
Expected Output: True
Explanation: Edges 1-2 and 1-2 form a 2-edge, 2-node component (edges ≤ nodes); assign one patient to slot 1 and the other to slot 2.
Input: ([1], [1], 1)
Expected Output: True
Explanation: A single patient whose two preferences are both slot 1: one edge, one node, so it takes slot 1.
Input: ([1, 2, 3], [2, 3, 1], 3)
Expected Output: True
Explanation: A 3-cycle: 3 edges, 3 nodes (edges ≤ nodes), so the cycle can be oriented to assign each patient a distinct slot.
Input: ([1, 2, 3, 1], [2, 3, 1, 2], 3)
Expected Output: False
Explanation: Four patients but only slots {1,2,3}: the component has 4 edges and 3 nodes, so two patients must collide — impossible.
Input: ([1, 1], [1, 1], 1)
Expected Output: False
Explanation: Two patients both only want slot 1: a 2-edge, 1-node component (edges > nodes), so they cannot both be seated.
Hints
- Treat each patient as an undirected edge between their two preferred slot-nodes.
- An assignment exists iff every connected component has at most as many edges (patients) as nodes (distinct slots) — a pseudoforest condition.
- Use union-find to group slots into components, then compare each component's edge count against its node count.