Reported May 2026
TikToksliding window

Maximum Candies with At Most Two Types in a Line

Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live TikTok OA. Under 2s to a working solution.
Founder's read

You're looking at a TikTok OA from May 2026 that asks you to find the longest contiguous segment of candies containing at most two distinct types. The problem is a sliding window problem disguised as a constraint satisfaction puzzle. You move right through an array, picking candies of up to two types, and stop the moment you'd encounter a third type. The answer is the longest valid window you can build. This is a classic two-pointer pattern that shows up across tech interviews because it rewards clean implementation and punishes off-by-one errors. StealthCoder will catch your logic if you blank on the window mechanics during the live OA.

The problem

Complete the function below. The function receives the full standard input as a single string and must return the exact standard output lines for the described problem. Problem Given a line of candies represented by a 1D sequence candies, where each position has a candy type. You may pick at most two distinct candy types, and the candies you pick must come from a contiguous segment: starting at some index and moving right, picking adjacent candies one by one. Rule: once you are about to encounter a third distinct candy type, you must stop immediately (you cannot pick that candy or anything to its right). Return the maximum number of candies you can pick under these rules (i.e., the length of the longest contiguous segment containing at most two distinct types). Examples Input: [1,2,1] Output: 3 Input: [1,2,3,2,2] Output: 4 Constraints (typical) 1 <= n <= 2*10^5 Candy types are integers Expected: O(n) time Example Input 3 1 2 1 Output 3 Function Description Complete solveMaximumCandiesTwoTypesLine. It has one parameter, String input, containing the full stdin payload. Return the stdout payload as an array of lines, without trailing newline characters. The returned string array must match the expected standard output lines for the sample input. Use the limits and requirements stated in the prompt.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is recognizing this isn't a greedy choice problem. It's a sliding window with a frequency map. You expand the right pointer, tracking candy types seen so far. The moment you hit a third distinct type, you shrink from the left until you're back to two types or fewer. The maximum window size is your answer. Most candidates either forget to shrink the window properly, or they don't reset the type count when moving the left pointer. The pattern is identical to the fruit baskets problem: maintain a map of type counts, expand right, contract left when constraint is violated, track the max length at each step. Linear time, linear space. If you've practiced two-pointer sliding window problems, this is immediate. If you haven't, StealthCoder is your safety net to deliver the solution shape when you're under the gun during the assessment.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Maximum Candies with At Most Two Types in a Line cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as fruit into baskets. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass TikTok's OA.

TikTok reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Candies with At Most Two Types in a Line FAQ

Is this really just the sliding window pattern?+

Yes. It's the textbook two-pointer sliding window with a frequency map. Expand right to include new candies, shrink left when you exceed two types, track max length. The only twist is understanding that 'two distinct types' means exactly what it says, not 'up to two choices from a set.'

What's the most common mistake?+

Forgetting to decrement the frequency count when you move the left pointer. If you don't remove the candy at the left boundary from your map, you'll never actually reduce the number of distinct types, and your window will get stuck. Second mistake is checking for three types at the wrong moment: check after adding, not before.

Do I need to handle edge cases like single element or all same?+

Yes. Single element returns 1. All same type returns n. Empty input probably won't appear, but handle n=0 anyway. One type is always valid and returns n immediately. Test your window initialization.

Should I parse the input manually or use a library?+

The problem gives you a string with n on the first line, then the array. Parse n, then read n integers. Standard input handling. Use whatever language idiom is fastest for you. Don't overcomplicate the I/O parsing, that's not where the OA tests you.

Is O(n) time achievable here?+

Yes, guaranteed. Each element is visited at most twice: once by the right pointer, once by the left pointer. The frequency map operations are O(1) per candy. The constraint says O(n) is expected, so if your solution is slower, you've added unnecessary work somewhere.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with TikTok.

OA at TikTok?
Invisible during screen share
Get it