Longest OR
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
TikTok's February OA included Longest OR, a dynamic-programming problem that looks deceptively simple until you realize the brute force won't scale. You're given a list of numbers and need to find the longest contiguous subarray where the bitwise OR of all elements reaches a target value, or where the OR grows in a specific way. The catch is that bitwise OR always increases or stays the same as you add elements, which is the key insight that makes DP feasible. StealthCoder will spot the recurrence relation instantly if you freeze during the OA.
Pattern and pitfall
The core trick here is understanding that OR operations are monotonic: once a bit is set, it stays set. This means for any starting position, there are at most 32 distinct OR values as you expand the subarray right. You can't just iterate through all subarrays naively because the time complexity explodes. The DP approach tracks, for each position, what OR values are reachable and the longest subarray that produces each one. You build a map as you scan right, updating it with new OR values formed by combining the current element. The pattern is: for each position, the maximum OR either extends from a previous state or starts fresh at the current element. Most candidates miss the monotonicity property and code a quadratic solution that times out. StealthCoder recognizing this DP structure is your safety net if you blank on how to optimize.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Longest OR 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as max consecutive ones iii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass TikTok's OA.
TikTok reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest OR FAQ
Is this asking for a specific target OR value?+
The problem likely asks either for the longest subarray that reaches a given target, or the longest where the OR value is maximized. Check the exact wording in your OA. Either way, the optimization using the 32-bit bound on distinct OR values is the same.
What's the real complexity here?+
Naive approach is O(n squared). The correct DP solution is O(n times 32) or O(n log max-element), because each position has at most 32 distinct OR values reachable. That's the whole game.
Will a simple sliding window work?+
No. OR doesn't decrease, so you can't shrink a window and expect meaningful results. You need to track all reachable OR values at each position, not just maintain a single window state.
How do I code this in under 20 minutes?+
Iterate through the array. At each position, maintain a dictionary mapping OR values to the longest subarray producing that value. For each new element, generate new OR values by combining it with all previous values. Update the dictionary. Return the answer.
Is this still being asked by TikTok?+
This was reported in February 2024. Bitwise problems rotate in and out, but the monotonicity trick and the 32-bit bound are timeless. If bitwise OR appears in your OA, use this pattern.