The Existence of a Substring in a String and Its Reverse interview question asks you to determine if there exists any substring of length 2 in a string s that also appears in the reverse of the string. In simpler terms, you need to check if there is a pair of adjacent characters s[i]s[i+1] such that the reversed pair s[i+1]s[i] also exists somewhere in the original string.
Companies like Rubrik use this String interview question as a quick check of your string manipulation and hashing skills. It's an "Easy" difficulty problem that evaluates whether you can efficiently search for patterns. It tests your ability to think about string properties—specifically, that a substring of length 2 in the reverse string is just a reversed pair of adjacent characters in the original string.
The problem can be solved using a Hash Set for O(N) efficiency.
n-2.s[i]s[i+1]:
s[i+1]s[i] is already in the set.s[i]s[i+1] if the string s[i+1]s[i] exists in the original string using a built-in search.String: s = "abcba"
String: s = "abcd"
Whenever a problem involves searching for "any" match, a Hash Set is usually the most efficient tool. It turns a potential O(N^2) search into an O(N) average time complexity.