FastPrepLast X Lines of a File
Problems/Last X Lines of a File

Last X Lines of a File

Google logoGoogle● EasyNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report describes a second coding round that asks to print the last x lines of a file. Here the function takes the text as a string, and we set the rules for newlines, a trailing newline, an empty file, and what to return. Our version is about 75% the same as the reported question.

Given file contents text and a nonnegative integer x, return the last at most x lines.

The newline character \n separates lines. Consecutive newlines create empty lines. A final newline terminates the preceding line and does not create an additional trailing empty line. Empty text contains zero lines. Preserve each returned line exactly.

Function

lastFileLines(text: String, x: int) → String[]

Examples

Example 1

text = "a\nb\nc"x = 2return = ["b","c"]

The final two of the three lines are b and c.

Example 2

text = "a\nb\n"x = 2return = ["a","b"]

The terminal newline closes line b without adding another line.

Example 3

text = ""x = 5return = []

An empty file has no lines.

Constraints

  • 0 <= text.length <= 1000000.
  • 0 <= x <= 1000000.
  • text may contain any characters except carriage return.

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

  • Run-Length Encoding and DecodingAirbnb · PHONE SCREENAirbnb interview questions
  • Check if a String Is a PalindromeCisco · PHONE SCREENCisco interview questions
  • Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
  • Reverse a StringAmazon · PHONE SCREEN
  • Reverse a String ManuallyNewgen Software · PHONE SCREEN
  • Reverse a String with Two PointersSnowflake · PHONE SCREENSnowflake interview questions
  • Palindrome with One DeletionWhatNot · PHONE SCREENWhatNot interview questions
  • Valid Word AbbreviationMeta · PHONE SCREENMeta interview questions
  • Reverse a StringPersistent Systems · PHONE SCREEN
  • Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions

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[] lastFileLines(String text, int x) {
        // Write your code here.
    }
}
Checking account…