Maximum Insertions Without Three Consecutive A
Updated
Problem statement
Write a function solution that, given a string S consisting of N characters, returns the maximum number of letters 'a' that can be inserted into S (including at the front and end of S) so that the resulting string doesn't contain three consecutive letters 'a'. If string S already contains the substring "aaa", then your function should return -1.
Function
solution(S: String) → intExamples
Example 1
S = "aabab"return = 3A string "aabaabaa" can be made.
Example 2
S = "dog"return = 8A string "aadaaoaagaa" can be made.
Example 3
S = "aa"return = 0No longer string can be made.
Example 4
S = "baaaa"return = -1There is a substring "aaa".
Constraints
- Write an efficient algorithm for the following assumptions:
Nis an integer within the range[1..200,000].- String
Sis made only of lowercase letters('a' - 'z').
More Tesla interview questions
- Count Zero-Sum FragmentsOA · Seen Jun 2026
- Maximum Number of Workers with Equal Left and Right ShoesOA · Seen Mar 2025
- Maximum Even-Sum Adjacent Pairs in a Circular ArrayOA · Seen Oct 2021
- Flatten a Branched List into a Doubly Linked ListPHONE SCREEN · Seen Sep 2026
- Reduce-Based Map and Filter PipelinePHONE SCREEN · Seen Sep 2026
- Resilient Fake API ClientPHONE SCREEN · Seen Sep 2026
- Alternate Positive and Negative ValuesONSITE INTERVIEW · Seen Aug 2026
- Enumerate Character Paths to a MarkerONSITE INTERVIEW · Seen Aug 2026
- Shared Counter Snapshot SumONSITE INTERVIEW · Seen Aug 2026
- Merge JSON Array FieldsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Maximum String OperationsMicrosoft · OAMicrosoft interview questions
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
- Longest Non-Decreasing Substring After Pair SwapsVisa · OAVisa interview questions
- Minimum Deletion Cost for Distinct Adjacent CharactersHSBC · OAHSBC interview questions
- Minimum Binary Swaps to a PalindromeAT&T · OAAT&T interview questions
- Robot Assembly Line ConflictsGoogle · OAGoogle interview questions
- Substring RemovalJP Morgan Chase · OAJP Morgan Chase interview questions
- Minimum Additions to Make a Valid ABC StringIBM · OAIBM interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Minimum Adjacent TranspositionsEpic · OAEpic interview questions
Tesla interview experiences
- Tesla Experienced Infrastructure SWE Interviews: 1 Experience
- Tesla Older SWE Intern Reports: 14 Experiences (Part 4 of 4)
- Tesla Embedded and SRE Intern Loops: 15 Experiences (Part 3 of 4)
- Tesla SWE and Data Intern Rounds: 10 Experiences (Part 2 of 4)
- Tesla Intern Python Interview Prep Guide
- 2017–2026 Tesla Intern OA / Interviews: 83 Experiences (Part 2 :)