Is Substring
Updated
Problem statement
You are given a string haystack and a string needle. Return true if needle appears inside haystack as a block of consecutive characters, and false otherwise.
Put another way, the answer is true when some index i exists such that the needle.length characters of haystack that begin at index i spell out needle exactly.
Write the character comparisons yourself: do not call a built-in substring search such as indexOf, contains, find, strings.Contains or Python's in operator on strings.
Function
isSubstring(haystack: String, needle: String) → booleanExamples
Example 1
haystack = "abacabab"needle = "abab"return = trueIndexes start at 0. The four characters of haystack starting at index 4 are "abab", which equals needle, so the answer is true. The block starting at index 0 is "abac", which differs from needle in its last character.
Example 2
haystack = "listing"needle = "lists"return = falseA block of five characters can start at index 0, 1 or 2, giving "listi", "istin" and "sting". None of them equals needle, so the answer is false.
Example 3
haystack = "home"needle = "homeowner"return = falseneedle has 9 characters and haystack has 4, so needle cannot fit inside haystack and the answer is false.
Constraints
1 <= haystack.length <= 1041 <= needle.length <= 104haystackandneedleconsist of lowercase English letters.
More Zillow Onsite Interview questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
- Count Duplicate CharactersONSITE INTERVIEW · Seen Oct 2020
- Symmetric TreeONSITE INTERVIEW · Seen Oct 2020
- Decision Tree Classifier from ScratchONSITE INTERVIEW · Seen Nov 2019
- Uniform to Normal NumbersONSITE INTERVIEW · Seen Nov 2019
- Check Each Word in a Character GridONSITE INTERVIEW · Seen Oct 2018
- Find Median from Data StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2018
Similar questions at other companies
- String CompressionShnoor International · ONSITE INTERVIEW
- Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
- Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
- Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
- Reverse Vowels of a StringGoogle · ONSITE INTERVIEWGoogle interview questions
- Valid Palindrome After NormalizationKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Count Binary SubstringsDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Palindrome Match PercentageApple · ONSITE INTERVIEWApple interview questions
- Reverse A StringNVIDIA · ONSITE INTERVIEWNVIDIA interview questions