Count Tested Devices After Test Operations - Problem

You are working as a quality assurance engineer for a tech company that manufactures smart devices. Your task is to test a batch of n devices in a specific order to determine how many are functional.

Given a 0-indexed integer array batteryPercentages of length n, where each element represents the battery percentage of a device, you need to test each device sequentially from index 0 to n-1.

Testing Process:

  • If a device has batteryPercentages[i] > 0:
    • Count it as tested (increment your counter)
    • ⚡ The testing process drains 1% battery from all remaining untested devices (indices i+1 to n-1)
    • 🛡️ Battery percentages cannot go below 0
  • If a device has batteryPercentages[i] = 0:
    • ❌ Skip it (dead battery, can't be tested)

Goal: Return the total number of devices that will be successfully tested.

Input & Output

example_1.py — Basic Testing Sequence
$ Input: [1, 1, 2, 1, 3]
Output: 3
💡 Note: Device 0 (battery 1): Tested ✓, drain others → [_, 0, 1, 0, 2]. Device 1 (battery 0): Skip. Device 2 (battery 1): Tested ✓, drain others → [_, _, _, 0, 1]. Device 3 (battery 0): Skip. Device 4 (battery 1): Tested ✓. Total: 3 devices tested.
example_2.py — All Dead Batteries
$ Input: [0, 0, 0]
Output: 0
💡 Note: All devices have 0% battery from the start, so none can be tested. Every device is skipped, resulting in 0 tested devices.
example_3.py — High Battery Devices
$ Input: [2, 1, 3]
Output: 2
💡 Note: Device 0 (battery 2): Tested ✓, drain others → [_, 0, 2]. Device 1 (battery 0): Skip. Device 2 (battery 2): Tested ✓. Total: 2 devices tested.

Constraints

  • 1 ≤ n ≤ 50
  • 0 ≤ batteryPercentages[i] ≤ 100
  • Each device must be tested in sequential order from index 0 to n-1

Visualization

Tap to expand
🔋 Device Testing LaboratoryConveyor Belt →85%D060%D10%D230%D3TestingStation⚡ Electromagnetic InterferenceDevices Tested2🔬 Optimization Formulaeffective_battery = original_battery - tests_completedNo need to modify arrays - calculate drain effect mathematically!
Understanding the Visualization
1
Initial Setup
All devices lined up with their initial battery percentages
2
Sequential Testing
Test each device in order, successful tests drain future devices by 1%
3
Optimization Insight
Instead of updating arrays, calculate effective battery = original - previous_tests
Key Takeaway
🎯 Key Insight: Instead of simulating each battery drain operation (O(n²)), we can mathematically calculate the cumulative effect in a single pass (O(n)).
Asked in
Amazon 25 Google 18 Microsoft 15 Apple 12
28.4K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen