FastPrepLongest Substring Without Repeating Characters
Problems/Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters

Salesforce logoSalesforce● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In the second technical round of a Salesforce MTS loop in Hyderabad, one of the two coding questions was the well-known task of finding the length of the longest substring with no repeated character. The candidate described it as a standard sliding window problem.

The post gives no examples or limits. Here the string has up to 5 * 10^4 printable ASCII characters, and an empty string returns 0.

Our version is about 95% the same as the reported question.

Given a string s, return the length of the longest substring of s in which no character appears more than once.

A substring is a run of characters that are next to each other in s. Uppercase and lowercase letters are different characters, and a space is a character too. If s is empty, return 0.

Function

lengthOfLongestSubstring(s: String) → int

Examples

Example 1

s = "xyzxyzyy"return = 3

The substring "xyz" has three different characters. Any substring of length 4 repeats a character; for example "xyzx" has x twice. So the answer is 3.

Example 2

s = "aaaa"return = 1

Every substring longer than one character repeats a, so the answer is 1.

Example 3

s = "dvdf"return = 3

The substring "vdf" has three different characters, and the whole string repeats d, so the answer is 3. Note that the best substring starts at the v, not at the second d.

Constraints

  • 0 <= s.length <= 5 * 10^4
  • s consists of English letters, digits, symbols and spaces (printable ASCII characters).

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
See Salesforce hiring insights
Saved
class Solution {
    public int lengthOfLongestSubstring(String s) {
        // Write your code here.
    }
}
Checking account…