Reverse a Character Array In Place
Reverse a Character Array In Place
Implement a function reverse_chars(chars: list[str]) -> None that reverses a list of characters in place. You are explicitly forbidden from using built-in reversal methods like reversed(), slicing (e.g., chars[::-1]), list.reverse(), or creating a new container whose size is proportional to the input.
The function should modify the input list directly and return None.
Constraints:
- The input list
charscan have a length between 0 and 100,000. - Each element in
charsis a single Unicode code point represented as a Python string. - The solution must achieve
O(n)time complexity andO(1)auxiliary space complexity.
Examples:
Input: chars = ["h", "e", "l", "l", "o"]
After: chars = ["o", "l", "l", "e", "h"]
Return: None
Input: chars = []
After: chars = []
Return: None
AI interview prep
Core dilemma
Efficiently modifying a data structure in place while adhering to strict memory constraints, requiring careful index management and avoiding auxiliary data structures.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Reverse a Character Array In Place
Reverse a Character Array In Place
Implement a function reverse_chars(chars: list[str]) -> None that reverses a list of characters in place. You are explicitly forbidden from using built-in reversal methods like reversed(), slicing (e.g., chars[::-1]), list.reverse(), or creating a new container whose size is proportional to the input.
The function should modify the input list directly and return None.
Constraints:
- The input list
charscan have a length between 0 and 100,000. - Each element in
charsis a single Unicode code point represented as a Python string. - The solution must achieve
O(n)time complexity andO(1)auxiliary space complexity.
Examples:
Input: chars = ["h", "e", "l", "l", "o"]
After: chars = ["o", "l", "l", "e", "h"]
Return: None
Input: chars = []
After: chars = []
Return: None