Python - Two pointer O(n) Solution

class Solution:
    # @param A : list of integers
    # @return a list of integers
    def square(a):
        return a*a

    def solve(self, A):
        # [-6, -3, -1, 2, 4, 5]
        # [0, 0, 0, 0, 0, 0,]
        #  [1, 4, 9, 16, 25, 36]
        n = len(A)
        res = [0] * n
        l = 0
        r = n - 1
        pos = n - 1
        last_value = 0

        while l < r:
            l_sq = Solution.square(A[l])
            r_sq = Solution.square(A[r]) 
            if l_sq > r_sq:
                res[pos] = l_sq
                last_value = r_sq
                l+=1
            else:
                res[pos] = r_sq
                last_value = l_sq
                r-=1
            pos-=1
        
        res[0] = last_value
        return res
Click here to start solving coding interview questions