FastPrepSelect First or Last K Stream Words
Problems/Select First or Last K Stream Words

Select First or Last K Stream Words

Google logoGoogle● MediumINTERNNEW GRADPHONE SCREENONSITE INTERVIEW

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: PhD Intern — The firsthand Google internship report progressively asks for the first or last k words from a list, then from a stream, and finally for distinct words. FastPrep uses a finite word array and an explicit mode, and defines last-distinct order by each word's final occurrence.

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 = 2mode = "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 = 3mode = "LAST"return = ["ant","cat","dog"]

The final three arrivals occupy indices 2 through 4.

Example 3

words = ["ant","bee","ant","cat","bee"]k = 3mode = "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.

More Google Phone Screen questions

  • 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
  • Logger Rate Limiter with Every-Arrival HistoryPHONE SCREEN · Seen Nov 2026
  • Minimum Total Layover WaitPHONE SCREEN · Seen Nov 2026
  • Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
  • Incident MonitorStripe · OAStripe interview questions
  • Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
  • Group AnagramsWorkday · PHONE SCREEN
  • Apply Operations to Make a String EmptyFlexport · PHONE SCREENFlexport interview questions
  • Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Group AnagramsEPAM · PHONE SCREENEPAM interview questions
  • Underline Shortest Unique SubstringsMoveworks · PHONE SCREENMoveworks interview questions
  • Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
  • Sliding Window Rate LimiterMercury · PHONE SCREEN

Interview reports that mention this question

  • Google New Grad NYC R2 Stream and Tree Rounds: 1 Experience
  • Google SWE and PhD Intern Coding: 14 Experiences (Part 3 of 3)

Google interview experiences

  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
  • Google AI Coding Interview and AI OA: 2 Experiences
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public String[] selectStreamWords(String[] words, int k, String mode) {
        // Write your code here.
    }
}
Checking account…