Frontend School

Battery Cell Health Rebalancing

DSA
medium

Battery Cell Health Rebalancing

Medium
Two Pointers
Arrays
In-place

Overview

In Tesla's battery manufacturing, optimizing the arrangement of individual battery cells within a pack is crucial for performance and longevity. Cells with varying health characteristics might need specific placements. This problem simulates a critical step in a rebalancing process where cells below a certain health threshold need to be grouped together for further inspection or specific integration, while healthier cells are grouped separately. Efficiently arranging these cells in-place minimizes manufacturing overhead and improves throughput.

What you need to do / Task

You are given an array healths representing the health values of individual battery cells and an integer threshold. Your task is to reorder the healths array in-place such that:

  1. All cells with a health value strictly less than threshold are moved to the beginning of the array.
  2. All cells with a health value greater than or equal to threshold are moved to the end of the array. The relative order of elements within each of these two partitions does not matter.

Your function should return the modified healths array.

Input & output

  • Input:
    • healths: An array of numbers (number[]), where each number represents the health score of a battery cell.
    • threshold: A number (number), representing the health score that separates "critical" from "healthy" cells.
  • Output:
    • The modified healths array (number[]) after partitioning.

Clarifications

  • Empty Input: If healths is an empty array, return an empty array.
  • All Critical/Healthy: The array might contain only critical cells (all < threshold) or only healthy cells (all >= threshold). Your solution should handle these cases correctly.
  • In-place: The modification must happen directly on the input healths array without creating a new array.

Examples

1

Example 1

Input:

healths = [85, 92, 60, 75, 95, 50, 80], threshold = 70

Output:

[60, 50, 85, 92, 95, 75, 80]

Note:

Cells with health < 70 (60, 50) are moved to the beginning. Cells with health >= 70 (85, 92, 75, 95, 80) are moved to the end. The order within [60, 50] and [85, 92, 75, 95, 80] does not matter, so other permutations are also valid outputs.

2

Example 2

Input:

healths = [100, 100, 100, 100], threshold = 70

Output:

[100, 100, 100, 100]

Note:

All cells have health >= 70, so no reordering is needed to satisfy the condition.

3

Example 3

Input:

healths = [30, 20, 10], threshold = 40

Output:

[30, 20, 10]

Note:

All cells have health < 40. They are already at the beginning of the array. The algorithm might swap elements among themselves, but the final state will satisfy the partitioning criteria.

4

Example 4

Input:

healths = [], threshold = 50

Output:

[]

Note:

An empty array remains empty after processing.

Constraints

Constraints & assumptions

  • 0 <= healths.length <= 10^5
  • 0 <= healths[i] <= 1000 for all i.
  • 0 <= threshold <= 1000
  • The solution must operate in O(N) time complexity, where N is the length of healths.
  • The solution must operate in O(1) auxiliary space complexity.

Out of scope

  • Maintaining the relative order of elements within the "critical" or "healthy" partitions is not required. Any order within these groups is acceptable.
  • Detailed error handling for invalid input types (e.g., healths not an array, threshold not a number) is not necessary. Assume valid inputs as per constraints.

What interviewers evaluate

  • Algorithmic Correctness: Does the solution correctly partition the array according to the specified criteria for all valid inputs?
  • Efficiency: Does the solution achieve optimal time and space complexity (O(N) time, O(1) space)?
  • Edge Case Handling: Is the solution robust to edge cases such as empty arrays, arrays with all elements on one side of the threshold, or arrays with a single element?
  • Code Quality: Is the code clean, readable, well-structured, and does it demonstrate a strong understanding of fundamental data structures and algorithms?

Loading editor...