Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2960: Count Tested Devices After Test Operations

In this guide, we solve Leetcode #2960 Count Tested Devices After Test Operations in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

You are given a 0-indexed integer array batteryPercentages having length n, denoting the battery percentages of n 0-indexed devices. Your task is to test each device i in order from 0 to n - 1, by performing the following test operations: If batteryPercentages[i] is greater than 0: Increment the count of tested devices.

Quick Facts

  • Difficulty: Easy
  • Premium: No
  • Tags: Array, Counting, Simulation

Intuition

The output depends on how often values appear.

Counting frequencies lets us answer queries in constant time afterward.

Approach

Count occurrences with a map or array, then compute the result from those counts.

This avoids repeated scans of the input.

Steps:

  • Count frequencies.
  • Use counts to compute result.
  • Return the computed value.

Example

Input: batteryPercentages = [1,1,2,1,3] Output: 3 Explanation: Performing the test operations in order starting from device 0: At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2]. At device 1, batteryPercentages[1] == 0, so we move to the next device without testing. At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1]. At device 3, batteryPercentages[3] == 0, so we move to the next device without testing. At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same. So, the answer is 3.

Python Solution

class Solution: def countTestedDevices(self, batteryPercentages: List[int]) -> int: ans = 0 for x in batteryPercentages: ans += x > ans return ans

Complexity

The time complexity is O(n)O(n)O(n), where nnn is the length of the array. The space complexity is O(1)O(1)O(1).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy