Solve common interview coding problems
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This set evaluates algorithmic problem-solving, data structure selection, and time/space complexity analysis across common patterns such as maximum subarray (linear and circular variants), nested string decoding, BST validation, task scheduling with cooldowns, sliding-window max-min constraints, and BFS shortest-paths; domain: Coding & Algorithms.
Part 1: Maximum Subarray Sum
Constraints
- 1 <= len(nums) <= 200000
- -1000000000 <= nums[i] <= 1000000000
- In fixed-width languages, use 64-bit integers for the answer.
Examples
Input: ([-2, 1, -3, 4, -1, 2, 1, -5, 4],)
Expected Output: 6
Explanation: The best subarray is [4, -1, 2, 1], which sums to 6.
Input: ([1],)
Expected Output: 1
Explanation: Single-element array.
Input: ([-5, -2, -8],)
Expected Output: -2
Explanation: When all numbers are negative, choose the least negative single element.
Input: ([5, -1, 5],)
Expected Output: 9
Explanation: The whole array is the best subarray.
Hints
- At each index, decide whether it is better to extend the previous subarray or start a new subarray here.
- Keep track of both the best subarray ending at the current position and the best answer seen so far.
Part 2: Maximum Subarray Sum in a Circular Array
Constraints
- 1 <= len(nums) <= 200000
- -1000000000 <= nums[i] <= 1000000000
- In fixed-width languages, use 64-bit integers for the answer.
Examples
Input: ([1, -2, 3, -2],)
Expected Output: 3
Explanation: The best subarray is [3]. Wrapping does not help.
Input: ([5, -3, 5],)
Expected Output: 10
Explanation: Take the wrapping subarray [5] + [5].
Input: ([-3, -2, -3],)
Expected Output: -2
Explanation: All values are negative, so choose the largest single element.
Input: ([7],)
Expected Output: 7
Explanation: Single-element array.
Hints
- The answer is either a normal maximum subarray, or a wrapping subarray that equals total_sum minus a minimum subarray.
- Be careful with the all-negative case: total_sum - min_subarray would incorrectly allow an empty subarray.
Part 3: Decode an Encoded String with Nesting
Constraints
- 1 <= len(s) <= 100000
- s contains lowercase English letters, digits, '[' and ']'
- The input encoding is valid
- The decoded output fits in memory
Examples
Input: ("3[a2[c]]",)
Expected Output: "accaccacc"
Explanation: a2[c] becomes acc, repeated 3 times.
Input: ("2[abc]3[cd]ef",)
Expected Output: "abcabccdcdcdef"
Explanation: Decode each bracketed block and keep trailing letters.
Input: ("10[a]",)
Expected Output: "aaaaaaaaaa"
Explanation: Repeat counts may have multiple digits.
Input: ("abc",)
Expected Output: "abc"
Explanation: A string with no brackets remains unchanged.
Hints
- When you see '[', save the current built string and the current repeat count.
- Repeat counts can have multiple digits, so build the number one digit at a time.
Part 4: Validate Whether a Binary Tree Is a BST
Constraints
- 0 <= number of nodes <= 100000
- Node values are integers
- Duplicates are not allowed in a valid BST
Examples
Input: ([2, 1, 3],)
Expected Output: True
Explanation: This tree satisfies the BST property.
Input: ([5, 1, 4, None, None, 3, 6],)
Expected Output: False
Explanation: The value 3 is in the right subtree of 5 but is smaller than 5.
Input: ([1, 1],)
Expected Output: False
Explanation: Duplicates violate the strict BST inequality.
Input: ([],)
Expected Output: True
Explanation: An empty tree is considered a valid BST.
Hints
- Checking only each node against its immediate children is not enough.
- Pass down a valid value range while traversing: every node must stay within its allowed lower and upper bounds.
Part 5: Task Scheduling with Cooldown
Constraints
- 1 <= len(tasks) <= 100000
- tasks[i] is an uppercase English letter A-Z
- 0 <= n <= 100000
Examples
Input: (["A", "A", "A", "B", "B", "B"], 2)
Expected Output: 8
Explanation: One optimal schedule is A, B, idle, A, B, idle, A, B.
Input: (["A", "A", "A", "B", "B", "B"], 0)
Expected Output: 6
Explanation: With no cooldown, tasks can be executed back-to-back.
Input: (["A", "A", "A", "B", "B", "B"], 1)
Expected Output: 6
Explanation: A, B, A, B, A, B uses no idle time.
Input: (["A"], 100)
Expected Output: 1
Explanation: Only one task needs one time unit.
Hints
- The most frequent task type determines the skeleton of the schedule.
- Count how many task types tie for the maximum frequency.
Part 6: Longest Subarray Under Max-Min Limit
Constraints
- 0 <= len(nums) <= 200000
- -1000000000 <= nums[i] <= 1000000000
- 0 <= limit <= 1000000000
Examples
Input: ([8, 2, 4, 7], 4)
Expected Output: 2
Explanation: The longest valid subarrays are [2, 4] and [4, 7].
Input: ([10, 1, 2, 4, 7, 2], 5)
Expected Output: 4
Explanation: One longest valid subarray is [2, 4, 7, 2].
Input: ([4, 2, 2, 2, 4, 4, 2, 2], 0)
Expected Output: 3
Explanation: With limit 0, all values in the window must be equal.
Input: ([], 3)
Expected Output: 0
Explanation: Empty input has no subarrays.
Hints
- Use a sliding window and expand the right boundary one step at a time.
- To query the current window maximum and minimum efficiently, maintain two monotonic deques.
Part 7: BFS Shortest Path in 1D
Constraints
- 1 <= N <= 200000
- 0 <= s, t < N
- Blocked positions are integers in the range [0, N-1]
Examples
Input: (5, [], 0, 4)
Expected Output: 4
Explanation: Move right four times.
Input: (7, [1, 5], 2, 4)
Expected Output: 2
Explanation: Path: 2 -> 3 -> 4.
Input: (5, [2], 0, 4)
Expected Output: -1
Explanation: Blocked position 2 splits the line into two disconnected parts.
Input: (1, [], 0, 0)
Expected Output: 0
Explanation: Start already equals target.
Hints
- This is an unweighted shortest-path problem, so breadth-first search is a natural fit.
- Each position should be visited at most once.
Part 8: BFS Shortest Path in 2D
Constraints
- 1 <= R * C <= 200000
- grid[r][c] is either '.' or '#'
- Start and target coordinates are integer pairs
Examples
Input: (["...", ".#.", "..."], (0, 0), (2, 2))
Expected Output: 4
Explanation: A shortest path goes around the blocked center cell.
Input: ([".#.", "###", "..."], (0, 0), (2, 2))
Expected Output: -1
Explanation: The start cell cannot reach the lower rows.
Input: (["."], (0, 0), (0, 0))
Expected Output: 0
Explanation: Start already equals target.
Input: (["#."], (0, 0), (0, 1))
Expected Output: -1
Explanation: The start cell is blocked, so the path is invalid.
Hints
- Use BFS from the start cell because every move has equal cost.
- Before exploring neighbors, verify that a cell is in bounds, not blocked, and not visited.