First Substring Occurrence
Updated
Problem statement
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) → intExamples
Example 1
haystack = "sadbutsad"needle = "sad"return = 0The first sad starts at index 0; a later occurrence does not change the answer.
Example 2
haystack = "leetcode"needle = "leeto"return = -1The 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