FastPrepFirst and Last K Words From a Stream
Problems/First and Last K Words From a Stream

First and Last K Words From a Stream

Google logoGoogle● EasyNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report goes from the first and last K words in a list to a stream, then discusses the first and last K words that do not repeat. We chose a combined return of two lists, that distinct means first occurrence, what happens when fewer than K remain, lowercase input, the examples, and the input limits. Our version is about 82% the same as the reported question.

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 1 through 100.
  • 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

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public String[][] firstAndLastKWords(String[] words, int k, boolean distinct) {
        // write your code here
    }
}
Checking account…