First and Last K Words From a Stream
Updated
Problem statement
Words arrive in the order given by words. Return two lists: the first k retained words and the last k retained words.
If distinct is false, retain every occurrence. If distinct is true, retain only the first occurrence of each word, preserving first-occurrence order.
When fewer than k words remain, return all of them in both lists. The two returned lists may overlap.
Function
firstAndLastKWords(words: String[], k: int, distinct: boolean) → String[][]Examples
Example 1
words = ["a","b","c","d"]k = 2distinct = falsereturn = [["a","b"],["c","d"]]The first two and final two stream occurrences are returned.
Example 2
words = ["a","b","a","c"]k = 2distinct = truereturn = [["a","b"],["b","c"]]The retained distinct sequence is [a,b,c].
Example 3
words = ["one"]k = 3distinct = falsereturn = [["one"],["one"]]Only one word is available, so both results contain it.
Constraints
1 <= words.length <= 100000.1 <= k <= 100000.- Every word has length from
1through100. - Words contain lowercase English letters.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Sliding Window Rate LimiterMercury · PHONE SCREEN
- Dynamic Longest-Waiting Unique Task QueriesAmazon · PHONE SCREEN
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Transcript Window MetricsSesame · PHONE SCREEN
- Drone API Sliding-Window Rate LimiterSkydio · PHONE SCREENSkydio interview questions
- Incident MonitorStripe · OAStripe interview questions
- Sliding Window Rate LimiterZipRecruiter · OA · ONSITE INTERVIEWZipRecruiter interview questions
- Capital Gains Tax from TradesArcesium · OAArcesium interview questions
- Price CheckBlue Origin · PHONE SCREEN