Check if one string is a substring of another
Filed under Goldman Sachs · Also reported at CouponDunia
Free read 1 of 3Use language-specific methods like 's2 in s1' or 's1.find(s2)'.
The question
Determine if string 's2' is a substring of string 's1'.
Constraints
- Strings consist of standard characters
- Empty substring is always contained
- Case sensitivity is assumed
Examples
- Input
- s1='hello', s2='ell'
- Output
- true
- Why
- 'ell' is found starting at index 1 of 'hello'.
Approach
Use language-specific methods like 's2 in s1' or 's1.find(s2)'.
Iterate through s1 and compare slices of length len(s2) with s2.
Mention KMP algorithm for O(N+M) time if asked for manual implementation.
Solution
O(N*M) worst case, O(N) average
def is_substring(s1, s2):
return s2 in s1
Tips
- What they are testing: Test with s2 longer than s1, empty s2, and s2 not present.
- Common mistake: Off-by-one errors in the loop bounds when checking substrings.
Related Goldman Sachs questions
Collected from public candidate posts and coding forums. Updated Nov 2026.