FastPrepGet Special Substring (MTS)
Problems/Get Special Substring (MTS)

Get Special Substring (MTS)

Salesforce logoSalesforce● EasyFULLTIMEOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

There are two types of characters in a particular language: special and normal. A character is special if its value is 1 and normal if its value is 0. Given string s, return the longest substring of s that contains at most k normal characters. Whether a character is normal is determined by a 26-digit bit string named charValue. Each digit in charValue corresponds to a lowercase letter in the English alphabet.

For clarity, the alphabet is aligned with charValue below:

s = "abcde" alphabet = abcdefghijklmnopqrstuvwxyz charValue = 10101111111111111111111111

The only normal characters in the language (according to charValue) are b and d. The string s contains both of these characters. For k = 2, the longest substring of s that contains at most k = 2 normal characters is 5 characters long, abcde, so the return value is 5. If k = 1 instead, then the possible substrings are {'b', 'd', 'ab', 'bc', 'cd', 'de', 'abc', 'cde'}. The longest substrings are 3 characters long, which would mean a return value of 3.

Function

getSpecialSubstring(s: String, k: int, charValue: String) → int

Complete the function getSpecialSubstring in the editor.

getSpecialSubstring has the following parameter(s):

  1. 1. String s: the input string
  2. 2. int k: the maximum number of normal characters allowed in a substring
  3. 3. String charValue: a string representing special or normal value of each letter of the alphabet, ascii[a-z]

Returns int: an integer that denotes the length of the longest substring of s with at most k normal characters

Examples

Example 1

s = "giraffe"k = 2charValue = "01111001111111111011111111"return = 3

Align the alphabet with charValue.

abcdefghijklmnopqrstuvwxyz 01111001111111111011111111

Normal characters are in the set {a, f, g, r}. All others are special.

For the string giraffe the longest possible substrings with 2 normal characters include gir, ira and ffe.

  • gir: The normal characters are g and r.
  • ira: The normal characters are r and a.
  • ffe: The normal characters are f and f.

The maximum length substring has a length of 3.

Example 2

s = "special"k = 1charValue = "00000000000000000000000000"return = 1

In this case, there can be only 1 normal character, and all characters are normal. The maximum length of a substring can only be of length 1. The possible substrings are s, p, e, c, i, a, and l.

Constraints

  • 1 ≤ length of s ≤ 10^5
  • 1 ≤ length of k ≤ the length of s
  • The length of charValue = 26
  • All values in charValue will be either 0 or 1

More Salesforce interview questions

  • Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
  • Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
  • 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
  • Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays With Product Equal to KOA · Seen Nov 2026
  • Minimum No-Repeat Segments After One Character RemovalOA · Seen Nov 2026
  • Get Message StatusOA · Seen Nov 2026
  • Group Identical Tree LevelsONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Count Repeated Request IDs (Data Science OA)McKinsey & Company · OAMcKinsey & Company interview questions
  • Find Sliding-Window Pattern Start IndicesVisa · OAVisa interview questions
  • Longest Cipher (Intuit India)Intuit · OAIntuit interview questions
  • Find RepetitionsPure Storage · OAPure Storage interview questions
  • Incident MonitorStripe · OAStripe interview questions
  • Maximum Valid Substring FrequencyMicrosoft · OAMicrosoft interview questions
  • Perfect SubstringAXQ Capital · OA
  • Same Substring Within BudgetJP Morgan Chase · OAJP Morgan Chase interview questions
  • Longest Distinct StringSuperhuman · OASuperhuman interview questions
  • K Smallest SubstringAkuna Capital · OAAkuna Capital interview questions

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
All Salesforce interview questionsSee Salesforce hiring insights
Saved
class Solution {
    public int getSpecialSubstring(String s, int k, String charValue) {
      // write your code here
    }

}
Checking account…