Reported February 2024
Metasliding window

Enumerating Narrative Sections

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

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

Meta's February OA asks you to count subarrays where at least K distinct elements appear with frequency 2 or higher. You're given a bucket of numbers (characters) and need to find all contiguous segments meeting a pairing threshold. This is a sliding window problem disguised as narrative fiction. If you blank on the approach, StealthCoder will feed you the two-pointer expansion strategy in real time.

The problem

\
Imagine you have an bucket of numbers, each representing a character in a story. Now, imagine each character can be duplicated, like twins in a tale. Your task is to count how many sequences of characters in the story contain at least a certain number of pairs of twins.\ \
In other words, you're looking for segments of the story where you can find enough pairs of characters that appear twice within that segment. These pairs must be distinct from each other and should occur at different points in the story.\

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is recognizing this as a sliding window variant where you're not just counting distinct elements, but tracking elements with frequency >= 2. Use two pointers: expand the right pointer to grow the window, shrink the left when you have more qualifying pairs than needed. Maintain a frequency map and a count of elements that have hit frequency 2. The common pitfall is overthinking the 'narrative' flavor text and missing that you only care about elements appearing at least twice in each valid subarray. StealthCoder spots this pattern immediately and can walk you through the expand-shrink rhythm if the logic gets fuzzy under time pressure.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Enumerating Narrative Sections 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as subarrays with k different integers. If you have time before the OA, drill that.

⏵ The honest play

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

Meta reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Enumerating Narrative Sections FAQ

Is this really just a sliding window problem?+

Yes. The story flavor is window dressing. You're finding all contiguous subarrays where at least K elements have frequency >= 2. Standard two-pointer approach: grow right, track frequencies, shrink left when constraints are satisfied.

What's the main trap candidates fall into?+

Confusing 'pairs of twins' with 'exactly 2 occurrences.' You want any element appearing 2 or more times. Also, many forget to count valid subarrays correctly when shrinking the window; you often add (right - left + 1) valid windows, not just one per iteration.

How do I track 'distinct pairs' without overcomplicating it?+

Use a frequency map and a separate counter for how many elements have reached frequency >= 2. When an element's frequency goes from 1 to 2, increment the counter. When it drops back below 2 (during shrink), decrement. Compare this count to K.

Can I solve this in one pass?+

No, you need sliding window. One pass with prefix sums won't work because the constraint is local to the window, not cumulative. Two pointers, O(n) time with a frequency map.

What if K is larger than possible distinct elements?+

The answer is 0. No subarray can satisfy the constraint. Check this edge case early. Also handle K = 0 (all subarrays qualify) and empty input gracefully.

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

OA at Meta?
Invisible during screen share
Get it