FastPrepFirst and Last Longest Repeat-Free Substrings
Problems/First and Last Longest Repeat-Free Substrings

First and Last Longest Repeat-Free Substrings

Adobe logoAdobe● MediumFULLTIMEINTERNPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem returns the earliest and the latest of the longest stretches of a string in which no character appears twice. In an Adobe MTS-1 report, the first-round coding question ended with printing one such stretch, the earliest or the latest one; before that came the length itself, which the report ties to a moving window. It gives no limits or examples.

Our choices: return both stretches as a pair (the same text twice when only one is longest), letters and digits only with case mattering, 1 to 10000 characters, and new examples.

The judged task matches the report at about 85%.

A repeat-free stretch of a string is a contiguous substring in which no character appears more than once.

Given a string s, find the greatest length of a repeat-free stretch. Several stretches may share that length. Return a list of two strings: the one that starts earliest in s, then the one that starts latest. If only one stretch has the greatest length, return it twice.

Function

longestUniqueStretches(s: String) → String[]

Examples

Example 1

s = "tacocat"return = ["taco","ocat"]

Both "taco" (indexes 0 to 3) and "ocat" (indexes 3 to 6) have four different letters. No longer stretch works: each of the three 5-letter stretches holds two cs. So the first longest stretch is "taco" and the last is "ocat".

Example 2

s = "xyzzy"return = ["xyz","xyz"]

The only 3-character stretch with no repeat is "xyz"; every longer stretch holds both zs. With one longest stretch, both answers are the same.

Example 3

s = "Aa1aA"return = ["Aa1","1aA"]

Upper and lower case letters are different characters, so "Aa1" has no repeat. "a1a" repeats a, and "1aA" is the last 3-character stretch with no repeat. Both 4-character stretches repeat a.

Constraints

  • 1 <= s.length <= 10^4
  • s contains only English letters (a-z, A-Z) and digits (0-9); upper and lower case count as different characters

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public String[] longestUniqueStretches(String s) {
    // Write your code here.
}
Checking account…