Reported February 2026
Googlehash table

Select First or Last K Stream Words

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

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

The mistake that sinks a first attempt on this Google OA, reported February 2026, is treating all four modes like one problem. They aren't. FIRST and LAST are plain slices. The two distinct modes are where people lose points, especially LAST_DISTINCT, where order depends on each word's final occurrence and not its first. The input goes up to 200000 words, so sloppy nested scans will time out. It's a hash-table problem with a little ordering logic on top. If you freeze on the distinct modes during the live assessment, StealthCoder runs invisibly as a safety net and gives you a working solution on screen.

The problem

You are given a finite stream of lowercase words, an integer k, and one of four modes:
FIRST: return the first k words.
LAST: return the last k words.
FIRST_DISTINCT: scan from left to right and return the first k different words, keeping their first-occurrence order.
LAST_DISTINCT: keep at most one copy of each word, select the k words whose final occurrences are latest, and return them in the order of those final occurrences.
For a distinct mode, if the stream contains fewer than k different words, return all different words in the required order.

Function
selectStreamWords(words: String[], k: int, mode: String) → String[]

Examples
Example 1
words = ["ant","bee","ant","cat","dog"]
k = 2
mode = "FIRST"
return = ["ant","bee"]
The first two stream items are returned, including any repetitions that would occur there.
Example 2
words = ["ant","bee","ant","cat","dog"]
k = 3
mode = "LAST"
return = ["ant","cat","dog"]
The final three arrivals occupy indices 2 through 4.
Example 3
words = ["ant","bee","ant","cat","bee"]
k = 3
mode = "LAST_DISTINCT"
return = ["ant","cat","bee"]
The final occurrences of ant, cat, and bee are at indices 2, 3, and 4, respectively.

Constraints
1 <= words.length <= 200000
1 <= k <= words.length
Each word contains 1 to 40 lowercase English letters.
mode is exactly FIRST, LAST, FIRST_DISTINCT, or LAST_DISTINCT.
The total number of characters in words is at most 1000000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

FIRST is words[0:k]. LAST is words[n-k:n]. FIRST_DISTINCT is one left-to-right pass with a hash set, appending unseen words until you hit k. LAST_DISTINCT is the trap. Walk the array right to left with a set, collecting a word the first time you see it, which is its final occurrence. Stop at k, then reverse the result so it's ordered by final occurrence ascending. Example 3 confirms it: ant, cat, bee. The common pitfall is running the first-distinct logic forward and taking the tail, which gives the wrong order. Another is calling list.contains inside a loop, which turns it quadratic at 200000 items. Use a set for O(n) total. Also handle fewer than k distinct words by returning what you have. Validate the mode string exactly. If you blank on the reverse-scan idea during the live OA, StealthCoder is the hedge that surfaces it.

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 Select First or Last K Stream Words 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

⏵ The honest play

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

Google 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.

Select First or Last K Stream Words FAQ

What's the trick in the LAST_DISTINCT mode?+

Scan from right to left with a hash set. The first time you see a word is its last occurrence. Collect until you have k words, then reverse the list. That gives you words ordered by final occurrence, earliest to latest, matching Example 3.

How hard is this Google OA question really?+

Easy to medium. There's no fancy algorithm. The difficulty is reading the four modes carefully and getting the ordering right in LAST_DISTINCT. Most failures come from rushing the spec, not from missing a data structure.

What time complexity do I need?+

O(n) with a hash set. With 200000 words, anything quadratic risks timing out. Total characters are capped at 1000000, so hashing the strings is cheap. Don't re-scan the array for each word.

What edge cases should I test?+

Fewer than k distinct words in a distinct mode, so return them all. k equal to the array length. Every word identical. A word repeated at both ends of the stream in LAST_DISTINCT. Also confirm FIRST and LAST keep repeats, as Example 1 states.

How do I prepare for this in 48 hours?+

Write the four modes as separate small branches and test each against the three examples. Practice the reverse-scan-then-reverse pattern once until it's automatic. Then do a couple of hash set and ordering problems for speed. Don't memorize, just know the shape.

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

OA at Google?
Invisible during screen share
Get it