FastPrepFirst Substring Occurrence
Problems/First Substring Occurrence

First Substring Occurrence

Expedia logoExpedia● MediumNEW GRADINTERNPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names the well-known task of finding the first place a substring occurs. We kept the inputs nonempty and lowercase, as in the linked reference, and added input limits. Our version is about 98% the same as the reported question.

Return the zero-based index of the first occurrence of needle as a contiguous substring of haystack. Return -1 if it does not occur.

Both strings are nonempty lowercase English strings. Matching is exact.

Function

firstOccurrence(haystack: String, needle: String) → int

Examples

Example 1

haystack = "sadbutsad"needle = "sad"return = 0

The first sad starts at index 0; a later occurrence does not change the answer.

Example 2

haystack = "leetcode"needle = "leeto"return = -1

The full needle does not occur.

Constraints

  • 1 <= haystack.length, needle.length <= 10000.
  • Both strings contain only lowercase English letters.

More Expedia Phone Screen questions

  • Find the Shortest Round TripPHONE SCREEN · Seen Jul 2026
  • Two SumPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2024
  • Merge Two Sorted Linked ListsPHONE SCREEN · ONSITE INTERVIEW · Seen May 2023
  • Assign CookiesPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
  • Kth Smallest Value in a BSTPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
  • Reverse a Singly Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2022
  • Climbing StairsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2021
  • Array Containment with MultiplicitiesPHONE SCREEN · Seen Nov 2019
  • Merge Sorted Arrays in PlacePHONE SCREEN · ONSITE INTERVIEW · Seen May 2019
  • Subtract the Next Smaller ValuePHONE SCREEN · Seen Oct 2018

Similar questions at other companies

  • Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Merge Weekday and Time IntervalsNextdoor · PHONE SCREENNextdoor interview questions
  • Business Account KYC Verification — Parts 1–5Stripe · PHONE SCREENStripe interview questions
  • Classify Password StrengthHackerRank · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
  • Read Across Sound TracksAirbnb · PHONE SCREENAirbnb interview questions
  • Sliding Items on a Circular RingIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
  • Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN
  • Longest Substring Without Repeating CharactersAngel One · PHONE SCREEN
  • Longest Substring Without Repeating CharactersMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Stateful Log Transform and SearchRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions

Interview reports that mention this question

  • Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
  • Expedia New Grad SDE Oldest Loops: 4 Experiences (Part 3 of 3)

Expedia interview experiences

  • Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
  • Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
  • Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
  • Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
All Expedia interview questionsSee Expedia hiring insights
Saved
class Solution {
    public int firstOccurrence(String haystack, String needle) {
        // Write your code here.
    }

}
Checking account…