PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Apple

Find longest uniform substring after k replacements

Last updated: Mar 29, 2026

Quick Overview

This question evaluates skills in string processing, frequency analysis, and algorithmic optimization within the Coding & Algorithms domain, focusing on practical application of linear-time, constant-space techniques.

  • Medium
  • Apple
  • Coding & Algorithms
  • Data Scientist

Find longest uniform substring after k replacements

Company: Apple

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a string s (ASCII, length up to 2e5) and integer k (0 ≤ k ≤ |s|), return the length of the longest substring that can be turned into all the same character by replacing at most k characters. Constraints and requirements: - Time O(n), space O(1) amortized (treat alphabet size as constant). - Explain why a sliding window with a running max‑frequency works and why shrinking the window preserves correctness even as the max frequency may lag. - Provide examples: s="AABABBA", k=1 ⇒ 4; s="aaabbc", k=2 ⇒ 5; s="abcd", k=1 ⇒ 2. - Implement in your preferred language and discuss edge cases (k=0, all identical chars, very large n).

Quick Answer: This question evaluates skills in string processing, frequency analysis, and algorithmic optimization within the Coding & Algorithms domain, focusing on practical application of linear-time, constant-space techniques.

Related Interview Questions

  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)
  • Rotate a Matrix In Place - Apple (medium)
  • Encode and Rebuild a Binary Tree - Apple (hard)
  • Wrap Matching Substrings in Bold Tags - Apple (medium)
Apple logo
Apple
Oct 13, 2025, 9:49 PM
Data Scientist
Technical Screen
Coding & Algorithms
3
0

Given a string s (ASCII, length up to 2e5) and integer k (0 ≤ k ≤ |s|), return the length of the longest substring that can be turned into all the same character by replacing at most k characters. Constraints and requirements:

  • Time O(n), space O(1) amortized (treat alphabet size as constant).
  • Explain why a sliding window with a running max‑frequency works and why shrinking the window preserves correctness even as the max frequency may lag.
  • Provide examples: s="AABABBA", k=1 ⇒ 4; s="aaabbc", k=2 ⇒ 5; s="abcd", k=1 ⇒ 2.
  • Implement in your preferred language and discuss edge cases (k=0, all identical chars, very large n).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Apple•More Data Scientist•Apple Data Scientist•Apple Coding & Algorithms•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.