Solve common string/DP/stack problems
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This set of four coding tasks evaluates proficiency in string algorithms, dynamic programming, sliding-window techniques, monotonic stack usage, and time/space complexity analysis.
Part 1: Longest Substring With All Distinct Characters
Constraints
- 0 <= len(s) <= 200000
- s may contain ASCII characters
- An O(n) solution is expected for large inputs
Examples
Input: "abcabcbb"
Expected Output: 3
Explanation: The longest valid substring is 'abc', which has length 3.
Input: ""
Expected Output: 0
Explanation: An empty string has no non-empty substring.
Input: "bbbbb"
Expected Output: 1
Explanation: Every substring longer than 1 contains repeated 'b'.
Input: "abba"
Expected Output: 2
Explanation: The longest distinct substrings are 'ab' and 'ba'.
Input: "tmmzuxt"
Expected Output: 5
Explanation: One longest distinct substring is 'mzuxt'.
Hints
- Try a sliding window with two pointers.
- Store the most recent index of each character so you can move the left boundary quickly when a duplicate appears.
Part 2: Largest Rectangle in a Histogram
Constraints
- 0 <= len(heights) <= 200000
- 0 <= heights[i] <= 1000000000
- The answer can be as large as about 2 * 10^14
Examples
Input: [2, 1, 5, 6, 2, 3]
Expected Output: 10
Explanation: The best rectangle uses the bars with heights 5 and 6, giving area 5 * 2 = 10.
Input: []
Expected Output: 0
Explanation: An empty histogram contains no rectangle.
Input: [6]
Expected Output: 6
Explanation: The only possible rectangle uses the single bar.
Input: [2, 4]
Expected Output: 4
Explanation: The best area is 4, either from height 4 with width 1 or height 2 with width 2.
Input: [0, 0, 0]
Expected Output: 0
Explanation: All bars have height 0, so every rectangle has area 0.
Hints
- For each bar, think about how far it can extend left and right before a shorter bar stops it.
- A monotonic increasing stack helps you compute areas exactly when a smaller height appears.
Part 3: Minimum Cost to Reach the Last Index With 1-2 Steps
Constraints
- 1 <= len(cost) <= 200000
- 0 <= cost[i] <= 1000000000
- The answer can be as large as about 2 * 10^14
Examples
Input: [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Expected Output: 6
Explanation: A cheapest path is 0 -> 2 -> 4 -> 6 -> 7 -> 9 with total cost 6.
Input: [5]
Expected Output: 5
Explanation: You start on the last index, so you pay only cost[0].
Input: [1, 2, 3]
Expected Output: 4
Explanation: The best path is 0 -> 2, so the cost is 1 + 3 = 4.
Input: [0, 0, 0, 0]
Expected Output: 0
Explanation: All visited indices cost 0.
Input: [10, 1, 1, 10, 1]
Expected Output: 12
Explanation: The best path is 0 -> 2 -> 4 with total cost 10 + 1 + 1 = 12.
Hints
- Let dp[i] be the minimum cost to reach index i.
- Each state depends only on the previous two states, so you can optimize space to O(1).
Part 4: Longest Palindromic Substring
Constraints
- 0 <= len(s) <= 2000
- s may contain arbitrary characters
- An O(n^2) solution is acceptable for this input size
Examples
Input: ""
Expected Output: ""
Explanation: The empty string has an empty longest palindromic substring.
Input: "a"
Expected Output: "a"
Explanation: A single character is always a palindrome.
Input: "cbbd"
Expected Output: "bb"
Explanation: The longest palindromic substring is 'bb'.
Input: "racecarxyz"
Expected Output: "racecar"
Explanation: The prefix 'racecar' is a palindrome of length 7.
Input: "forgeeksskeegfor"
Expected Output: "geeksskeeg"
Explanation: The longest palindromic substring is 'geeksskeeg'.
Hints
- Every palindrome is centered either on a character or between two characters.
- Expand outward from each possible center instead of checking every substring.