Longest Common Prefix Length
Updated
Problem statement
You are given two strings a and b.
Return the length of their longest common prefix: the longest string that begins at index 0 in both inputs. Character comparisons are case-sensitive.
Function
longestCommonPrefixLength(a: String, b: String) → intExamples
Example 1
a = "flower"b = "flow"return = 4Both strings begin with flow, and the second string ends after four characters.
Example 2
a = "abc"b = "xyz"return = 0The first characters differ, so the common prefix is empty.
Constraints
0 <= a.length, b.length <= 105- The combined input length is at most
2 * 105. - Both strings contain printable ASCII characters.
More Visa Online Assessment questions
- Count Prime Values in SubtreesOA · Seen Nov 2026
- Lock CodeOA · Seen Nov 2026
- Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
- Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
- Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
- Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
- Count Valid Space-Delimited WordsOA · Seen Oct 2026
- Earliest Common Meeting StartOA · Seen Oct 2026
- Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
- Maximum Equalized Substrings After One Adjacent SwapOA · Seen Sep 2026
Similar questions at other companies
- Minimum Changes for Distinct Adjacent CharactersHeadlands Technologies · OAHeadlands Technologies interview questions
- Theme Park Shuttle Wait TimeAirbnb · OAAirbnb interview questions
- REST API: Country CodesHackerRank · OAHackerRank interview questions
- Fill One Exact-Length Binary WindowAmazon · OA
- Remove Vowels from a StringHSBC · OAHSBC interview questions
- Branch with the Most FilesCapital One · OACapital One interview questions
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Maximum Ones After K OperationsGoldman Sachs · OAGoldman Sachs interview questions
- Total Each Letter's Count in an Encoded StringIBM · OAIBM interview questions
- Robot Stops Left or Right (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions