Solve four OA algorithm problems
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This multi-part exercise evaluates algorithmic problem-solving across combinatorics and geometry (counting axis-aligned square subgrids), string reordering under adjacency constraints (minimum adjacent swaps to form a palindrome), deterministic grid-walk simulation with teleportation and cycle detection, and prefix-sum based range optimization for arrays, and it belongs to the Coding & Algorithms domain. These problems are commonly asked to assess efficiency under large constraints, correctness in handling edge cases and invariants, and the ability to blend conceptual understanding with practical algorithm design, requiring both conceptual reasoning and applied implementation skills.
Count Square Subgrids (Multiple Queries)
Constraints
- 1 <= q <= 2 * 10^5
- 1 <= R, C <= 10^9
- Answers can exceed 64 bits for large grids; use big-integer arithmetic where the language requires it.
Examples
Input: ([[2, 2], [1, 1], [3, 2]],)
Expected Output: [5, 1, 8]
Explanation: 2x2: four 1x1 plus one 2x2 = 5. 1x1: one square. 3x2: six 1x1 plus two 2x2 = 8.
Input: ([[1, 1]],)
Expected Output: [1]
Explanation: A single cell contains exactly one 1x1 square.
Input: ([[3, 3]],)
Expected Output: [14]
Explanation: nine 1x1 + four 2x2 + one 3x3 = 14.
Input: ([[5, 3], [4, 4]],)
Expected Output: [26, 30]
Explanation: 5x3: 15 (1x1) + 8 (2x2) + 3 (3x3) = 26. 4x4: 16 + 9 + 4 + 1 = 30.
Input: ([],)
Expected Output: []
Explanation: No queries means an empty result list.
Input: ([[1000000000, 1000000000]],)
Expected Output: [333333333833333333500000000]
Explanation: Large-grid stress test; the answer overflows 64 bits, confirming big-integer math is required.
Hints
- A k x k square fits in (R - k + 1) * (C - k + 1) positions, valid for 1 <= k <= min(R, C).
- Sum that product over k. Expand it and use closed forms: sum of 1..m = m(m+1)/2 and sum of squares = m(m+1)(2m+1)/6 to get O(1) per query.
- Let a = R+1, b = C+1, m = min(R,C). Then the answer is a*b*m - (a+b)*S1 + S2 where S1 and S2 are those two closed forms.
Minimum Adjacent Swaps to Make a Palindrome
Constraints
- 0 <= len(s); the string may contain any characters (the OA used binary 'O'/'I').
- Return -1 when the character multiset cannot form a palindrome.
- At most one character may have an odd frequency for a palindrome to exist.
Examples
Input: ("OIIO",)
Expected Output: 0
Explanation: Already a palindrome, so zero swaps.
Input: ("OOI",)
Expected Output: 1
Explanation: Swap the last two characters to get 'OIO'.
Input: ("mamad",)
Expected Output: 3
Explanation: Classic case; three adjacent swaps yield 'madam'.
Input: ("aabb",)
Expected Output: 2
Explanation: Two swaps produce 'abba' (or 'baab').
Input: ("ab",)
Expected Output: -1
Explanation: Two distinct single characters can never form a palindrome.
Input: ("a",)
Expected Output: 0
Explanation: A single character is already a palindrome.
Input: ("OIOII",)
Expected Output: 2
Explanation: Odd length with one odd-count character; the greedy minimum is two swaps.
Hints
- First check feasibility: count characters with odd frequency. Even-length strings need zero; odd-length strings need exactly one.
- Work from both ends inward. For the left character s[i], scan leftward from the right pointer to find its nearest twin, then bubble that twin to position j with adjacent swaps.
- If s[i] is itself the unique odd character (no twin to its right), swap it one step toward the center and retry; this defers it to the middle.
Row-Major Grid Walk with Obstacles, Teleporters, and Loop Detection
Constraints
- 1 <= n, m.
- Stepping onto a blocked cell (including a teleporter destination) returns -1.
- A teleport happens on landing and is part of the same step.
- If a cell is revisited (cycle) without reaching the target, return -2.
Examples
Input: (2, 2, [], [])
Expected Output: 3
Explanation: Row-major path (0,0)->(0,1)->(1,0)->(1,1) takes 3 steps.
Input: (2, 2, [[0, 1]], [])
Expected Output: -1
Explanation: The first step lands on the blocked cell (0,1).
Input: (1, 1, [], [])
Expected Output: 0
Explanation: Start cell is already the target, so zero steps.
Input: (2, 2, [], [[[0, 1], [1, 1]]])
Expected Output: 1
Explanation: Step onto teleporter (0,1) and warp to target (1,1) in a single step.
Input: (2, 2, [], [[[0, 1], [0, 0]]])
Expected Output: -2
Explanation: Teleporter on (0,1) sends you back to (0,0), an already-visited cell -> cycle.
Input: (3, 3, [], [])
Expected Output: 8
Explanation: Row-major sweep over all nine cells reaches (2,2) in 8 steps.
Input: (3, 3, [[1, 1]], [])
Expected Output: -1
Explanation: The walk reaches the blocked center cell (1,1) and stops.
Hints
- Store blocked cells in a set and teleporters in a dict mapping source -> destination for O(1) lookups.
- Simulate one step at a time following the row-major rule; after landing, check 'blocked' first, then apply the teleport (which may land on another blocked cell).
- Keep a visited set of post-teleport positions. Re-landing on a visited cell means an inescapable cycle -> return -2; running off the end of the grid without reaching the target is also -2.
Longest Subarray Under a Prefix-Sum Inequality
Constraints
- a[i] >= 0 for all i, so the prefix sums are non-decreasing.
- k may be negative, in which case some or all subarrays can be invalid.
- 0 <= l < r <= n; return 0 when no subarray satisfies the inequality.
Examples
Input: ([1, 2, 3], 5)
Expected Output: 2
Explanation: Subarray [1,3) has pref[3]-2*pref[1]=6-2=4<=5 with length 2; no length-3 window qualifies.
Input: ([1, 2, 3], 0)
Expected Output: 1
Explanation: Only single-element windows where pref[r]-2*pref[l]<=0 work; max length is 1.
Input: ([0, 0, 0], 0)
Expected Output: 3
Explanation: All prefix sums are 0, so the whole array satisfies 0<=0; length 3.
Input: ([5], 4)
Expected Output: 0
Explanation: pref[1]-2*pref[0]=5>4, so no valid subarray; answer 0.
Input: ([5], 5)
Expected Output: 1
Explanation: pref[1]-2*pref[0]=5<=5, so the single element qualifies; length 1.
Input: ([2, 1, 4, 1], 3)
Expected Output: 2
Explanation: The best feasible window has length 2 under the constraint.
Input: ([], 0)
Expected Output: 0
Explanation: Empty array has no subarray; answer 0.
Hints
- Build the prefix-sum array and rewrite the constraint as 2*pref[l] >= pref[r] - k.
- Since a[i] >= 0, pref is non-decreasing, so as r increases the minimal feasible l never decreases - a textbook two-pointer setup.
- For each r, advance l until the inequality holds, then the candidate length is r - l; keep the maximum. If l reaches r, no subarray ending at r works.