Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
I think using absolute value can give a simpler solution. Four less lines of cod...

Daniel Snider

Dec 22, 2022

I think using absolute value can give a simpler solution. Four less lines of code in python:

def square_sorted_array(arr): squares = [] left_idx = 0 right_idx = len(arr) - 1

while left_idx arr[right_idx]: squares.insert(0, arr[left_idx] * arr[left_idx]) left_idx += 1

else: squares.insert(0, arr[right_idx] * arr[right_idx]) right_idx -= 1

return squares

0

1

Comments
Comments

On this page

Problem Statement

Solution

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity