FastPrepLongest Substring with Even Occurrences
Problems/Longest Substring with Even Occurrences

Longest Substring with Even Occurrences

Deloitte logoDeloitte● MediumNEW GRADOA

Updated Sep 2026

QuestionFunctionExamples

Problem statement

Write a function:

def solution(S)

that, given a string S consisting of N lowercase English letters, returns the length of the longest substring in which every letter occurs an even number of times. A substring is defined as a contiguous segment of a string. If no such substring exists, return 0.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..100,000];
  • string S consists only of lowercase letters ('a'-'z').

Function

solution(S: String) → int

Examples

Example 1

S = "bdaaadadb"return = 6

Substrings in which every letter occurs an even number of times are "aa", "adad", "daaadad" and "aaadad". The length of the longest of them is 6.

Example 2

S = "abacb"return = 0

There is no non-empty substring in which every letter occurs an even number of times.

Example 3

S = "zthtzh"return = 6

Every letter in the whole string occurs an even number of times.

More Deloitte interview questions

  • Second-Highest Distinct SalaryPHONE SCREEN · Seen Jul 2026
  • Check AnagramsONSITE INTERVIEW · Seen Jun 2026
  • Seventh Nearest PalindromeOA · Seen May 2026
  • Minimum Deletions for a Non-PalindromeOA · Seen Jul 2025
  • Minimum-Cost Solar Rooftop AllocationOA · Seen Jul 2025
  • Pair Elements with Even SumsOA · Seen Jul 2025
  • Count Divisible PermutationsOA · Seen Jan 2025
  • Largest Element in an ArrayOA · Seen Nov 2024
  • Install Carbon FiltersOA · Seen Oct 2023
  • Maximum Sum RectangleOA · Seen Apr 2022

Similar questions at other companies

  • Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
  • Password Clues and the Next CommandOpenAI · OAOpenAI interview questions
  • Shortest Digit Prefix for Target MultisetsVisa · OAVisa interview questions
  • Longest Balanced Substring After One SwapTekion · OATekion interview questions
  • Count Special StringsAmazon · OA
  • Extraordinary SubstringsPalantir · OAPalantir interview questions
  • Count Subarrays With Given XORInMobi · OAInMobi interview questions
  • Set Total Palindrome Transformation CostIntuit · OAIntuit interview questions
  • Number of Divisible Substrings (For MLE)Wayfair · OAWayfair interview questions
  • Palindrome-Rearrangeable PairsExpedia · OAExpedia interview questions

Interview reports that mention this question

  • Deloitte New Grad Tech OA and Interview Questions: 14 Experiences
All Deloitte interview questionsSee Deloitte hiring insights
Saved
class Solution {
    public int solution(String S) {
      // write your code here
    }

}
Checking account…