PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Solve common interview coding problems

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given several independent coding tasks (typical of SWE/MLE interview rounds). For each task, design an algorithm and describe the time/space complexity. ## 1) Maximum subarray sum (Kadane-style) **Input:** an integer array `nums` of length `n` (may contain negatives). **Task:** return the maximum possible sum of any **non-empty contiguous** subarray. **Constraints (typical):** `1 <= n <= 2e5`, `-1e9 <= nums[i] <= 1e9`. --- ## 2) Maximum subarray sum in a circular array **Input:** an integer array `nums` of length `n`, interpreted as circular (after index `n-1` comes index `0`). **Task:** return the maximum possible sum of any **non-empty** contiguous subarray where the subarray may wrap around the end to the beginning. **Constraints (typical):** `1 <= n <= 2e5`. --- ## 3) Decode an encoded string with nesting **Input:** a string `s` consisting of lowercase letters, digits, and brackets, e.g. `"3[a2[c]]"`. Encoding rule: `k[encoded_string]` means `encoded_string` repeated `k` times. Encodings may be nested. **Task:** return the decoded string. **Constraints (typical):** decoded length can be large; avoid quadratic behavior. --- ## 4) Validate whether a binary tree is a BST **Input:** the root of a binary tree. **Task:** return `true` if it is a valid binary search tree (BST), otherwise `false`. BST definition: for every node, all keys in the left subtree are **strictly less** than the node’s key, and all keys in the right subtree are **strictly greater**; both subtrees must also be BSTs. --- ## 5) Task scheduling with cooldown **Input:** - a list of tasks `tasks` where each task is an uppercase letter `A`–`Z` - an integer cooldown `n >= 0` Each task takes 1 unit time. The same task type must be separated by at least `n` time units (idle time allowed). **Task:** return the minimum total time units to finish all tasks. --- ## 6) Longest subarray under max-min constraint (sliding window) **Input:** an integer array `nums` and an integer `limit`. **Task:** return the length of the longest contiguous subarray such that: \[ \max(subarray) - \min(subarray) \le limit \] Follow-ups often compare approaches: - brute force - maintaining min/max with heaps - optimizing to linear time --- ## 7) BFS shortest path follow-up: 1D to 2D ### 7a) 1D **Input:** a 1D line of `N` positions (0..N-1), with some blocked positions, a start `s`, and a target `t`. From position `i`, you may move to `i-1` or `i+1` if in bounds and not blocked. **Task:** find the minimum number of moves from `s` to `t` (or report unreachable). ### 7b) 2D follow-up **Input:** an `R x C` grid with blocked cells, start cell, and target cell. Moves are 4-directional. **Task:** find the minimum number of moves from start to target (or report unreachable).

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

Given a non-empty integer array nums, return the largest possible sum of any non-empty contiguous subarray.

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

  1. At each index, decide whether it is better to extend the previous subarray or start a new subarray here.
  2. 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

Given an integer array nums interpreted as circular, return the maximum possible sum of any non-empty contiguous subarray. The chosen subarray may wrap from the end of the array back to the beginning.

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

  1. The answer is either a normal maximum subarray, or a wrapping subarray that equals total_sum minus a minimum subarray.
  2. 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

You are given a string s containing lowercase letters, digits, and brackets. The encoding rule is k[encoded_string], meaning encoded_string repeated k times. Encodings may be nested. Return the fully decoded string.

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

  1. When you see '[', save the current built string and the current repeat count.
  2. 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

A binary tree is given as a compact level-order list using None for missing children. Return True if the tree is a valid binary search tree (BST), otherwise return False. A BST requires every value in the left subtree to be strictly less than the node value, and every value in the right subtree to be strictly greater.

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

  1. Checking only each node against its immediate children is not enough.
  2. 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

You are given a list of tasks represented by uppercase letters A-Z and a non-negative integer n. Each task takes 1 unit of time, and identical task types must be separated by at least n time units. Idle slots are allowed. Return the minimum total time needed to finish all tasks.

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

  1. The most frequent task type determines the skeleton of the schedule.
  2. Count how many task types tie for the maximum frequency.

Part 6: Longest Subarray Under Max-Min Limit

Given an integer array nums and an integer limit, return the length of the longest contiguous subarray such that the difference between the maximum and minimum values in that subarray is at most 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

  1. Use a sliding window and expand the right boundary one step at a time.
  2. To query the current window maximum and minimum efficiently, maintain two monotonic deques.

Part 7: BFS Shortest Path in 1D

You are given a 1D line of positions 0 to N-1, some blocked positions, a start position s, and a target position t. From position i, you may move to i-1 or i+1 if the next position is in bounds and not blocked. Return the minimum number of moves needed to reach t from s, or -1 if it is impossible.

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

  1. This is an unweighted shortest-path problem, so breadth-first search is a natural fit.
  2. Each position should be visited at most once.

Part 8: BFS Shortest Path in 2D

You are given an R x C grid, a start cell, and a target cell. Open cells are marked with '.', blocked cells with '#'. You may move up, down, left, or right. Return the minimum number of moves from start to target, or -1 if unreachable.

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

  1. Use BFS from the start cell because every move has equal cost.
  2. Before exploring neighbors, verify that a cell is in bounds, not blocked, and not visited.
Last updated: May 11, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Solve common string/DP/stack problems - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)