Count how many times the class monitor is replaced

Quick Overview

Count how many times a class monitor changes as students with numeric ranks visit in order and a strictly lower rank takes over. It tests a single pass with a running minimum, strict comparison on ties, and not counting the first student as a replacement.

Count how many times the class monitor is replaced

Company: Deloitte

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of integers `ranks` representing the ranks of students visiting an office in order, return the number of times the class monitor is replaced. The first student to visit becomes the initial monitor. The monitor is replaced whenever a visiting student has a strictly better (lower) rank than the current monitor. Note that the initial assignment of the first student as monitor is not counted as a replacement. ### Function Signature ```python def count_monitor_replacements(ranks: list[int]) -> int: ``` ### Constraints - `1 <= len(ranks) <= 10^5` - `1 <= ranks[i] <= 10^9` ### Examples ```text Input: ranks = [5,3,8,2,7,1,4] Output: 3 ``` The initial monitor is 5. They are replaced by 3, then by 2, then by 1, for a total of 3 replacements. The students ranked 8, 7 and 4 each rank worse than the monitor at the time they visit. ```text Input: ranks = [10,10,10,10] Output: 0 ``` No student has a strictly lower rank than the initial monitor (10), so no replacements occur.

Overview: Count how many times a class monitor changes as students with numeric ranks visit in order and a strictly lower rank takes over. It tests a single pass with a running minimum, strict comparison on ties, and not counting the first student as a replacement.

Deloitte logo
Deloitte
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers ranks representing the ranks of students visiting an office in order, return the number of times the class monitor is replaced.

The first student to visit becomes the initial monitor. The monitor is replaced whenever a visiting student has a strictly better (lower) rank than the current monitor.

Note that the initial assignment of the first student as monitor is not counted as a replacement.

Function Signature

def count_monitor_replacements(ranks: list[int]) -> int:

Constraints

  • 1 <= len(ranks) <= 10^5
  • 1 <= ranks[i] <= 10^9

Examples

Input: ranks = [5,3,8,2,7,1,4]
Output: 3

The initial monitor is 5. They are replaced by 3, then by 2, then by 1, for a total of 3 replacements. The students ranked 8, 7 and 4 each rank worse than the monitor at the time they visit.

Input: ranks = [10,10,10,10]
Output: 0

No student has a strictly lower rank than the initial monitor (10), so no replacements occur.