PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string-processing and algorithmic problem-solving skills, including handling incremental updates, counting substrings impacted by character replacements, and performance-aware simulation.

  • medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Determine Rounds for Star Substring Threshold

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

##### Question Given a string S, an offset array O (replacement order), and an integer M, repeatedly replace S[O[i]] with '*'. Determine the minimum number of rounds needed so that the count of substrings containing at least one '*' is at least M.

Quick Answer: This question evaluates string-processing and algorithmic problem-solving skills, including handling incremental updates, counting substrings impacted by character replacements, and performance-aware simulation.

You are given a string S of length n, an integer array order of length n that is a permutation of [0..n-1], and an integer M. In round k (for k >= 1), you replace S[order[k-1]] with '*'. After k rounds, define F(k) as the number of substrings of S that contain at least one '*'. Return the minimum k in [0..n] such that F(k) >= M. If no such k exists, return -1. Substrings are contiguous and defined by indices [i..j] with 0 <= i <= j < n.

Constraints

  • 1 <= n <= 2 * 10^5
  • order is a permutation of [0..n-1]
  • S consists of lowercase English letters (no '*' initially)
  • 0 <= M <= 10^14
  • 0-based indexing for order
  • Return -1 if M > n*(n+1)/2

Hints

  1. F(k) = total_substrings - substrings_without_any_star.
  2. Substrings without star after k rounds are the sum over contiguous blocks of unstarred positions: for a block of length L, it contributes L*(L+1)/2.
  3. F(k) is non-decreasing in k, enabling binary search on k.
  4. For a feasibility check at k, mark the first k starred positions and scan once to sum the star-free blocks.
Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,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
  • AI Coding 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.

Related Coding Questions

  • Minimum Path Length Through a Grid With One Allowed Cell Conversion - Amazon (medium)
  • Circular Drone Hub Delivery Route - Amazon (hard)
  • Leaf Domain Cumulative Scores - Amazon (medium)
  • Kth Largest Perfect Binary Subtree - Amazon (medium)
  • Find Conflicting Events - Amazon (medium)