Amazon
  • Introduction
  • Phone Interview I
    • 53.Reverse Words in a String
    • 31.Partition Array
  • Phone Interview II
    • 167.Add Two Numbers
    • 88.Lowest Common Ancestor
  • Onsite I
    • 655.Big Integer Addition
    • 221.Add Two Numbers II
  • Onsite II
    • 158.Two Strings Are Anagrams
    • 171.Anagrams
    • 386.Longest Substring with At Most K Distinct Characters
  • Onsite III
    • 479.Second Max of Array
    • 589.Connecting Graph
  • Onsite IV
    • 532.Reverse Pairs
  • 2022
    • OA
      • work simulation
      • Greyness
      • NearestRetailer
      • Sum of Scores of Subarray
      • StrengthOfPassword
      • ProductOf1
      • Move 0/1 InArray
      • Max deviation among all substrings
      • AWS power consumption
      • searchWordResultWord
      • maxOperationOfString
      • MinHealthGame
      • EarliestMonth
      • Package ship
      • RatingConsectiveDecresing
      • LinkedListSum
      • MovingBoxes
      • ValidString
      • MaxValueAfterRemovingFromString
      • Subtree with Maximum Average
    • VO
      • 2022.3
    • BQ
      • doc
      • 2022.4
      • Freq Question
      • 11大类BQ和Follow-ups
      • Page 1
      • BQ 100
      • 35 behavioral questions asked in 95% of Amazon interviews with examples
      • Page 2
      • 反向BQ
    • LP
      • LP-1
      • LP-2
    • SD
      • Design Amazon Prime Video Recommendation System
      • Amazon Order system
    • OOD
      • Linux Find Command
      • Amazon Locker
    • AWS Identity call
    • Interviews
Powered by GitBook
On this page

Was this helpful?

  1. 2022
  2. OA

ProductOf1

https://leetcode.com/discuss/interview-question/1655441/Amazon-or-OA

A question about Amazon student badges but ultimately the problem was given an arr of -1 or 1, return the maximum subarray length with a product of 1. The array is of size 2 and above and only contains -1 and 1

e.g arr = [-1,-1,1,1,-1], return 4, since index 0 to 3 have the max length with product equal to 1

Similar as LC 1567

Solution 1:

  1. Use a sliding window index , called end

  2. initialize count of "-1" as 0

  3. while end is less than the arr size, increment end

  4. if arr[end] == "-1", Save the index of this first occurence of "-1" only once

And always increment the count of -1

  1. if count is even , ans = max(ans,end +1) If count is odd and count != 1 ans = max( ans, end - (index_of_1st_occurence_of_-1 + 1) + 1)

Because if the count of -1 is currently odd, the region between the first ever -1 and index end will be even.

Solution 2:

Intuition is traverse input array of size n once and ONLY maintain integer count of -1s and append their index locations in queue

  1. if integer count is even, return len(input array) # we found equilibrium

  2. if integer count is odd, we want to find a subarray such that we must remove the leftmost or rightmost -1 that is breaking equilibrium ... **so max length of either of these two subarrays **

  • arr[ 0 : index of latest queue item (rightmost minus 1) -1 ]

  • arr[ index of first queue item (leftmost minus 1) +1 : n]

note: equilibrium just means that there are 0 or even number of -1s .. we dont really care about number of +1s

from collections import deque

def solve(arr):
    l = 0
    r = len(arr)
    q = deque()
    cnt_minus1 = 0
    
    for i in range(r):
    	if arr[i] == -1:
    		cnt_minus1 = cnt_minus1 + 1
    		q.append(i) 
    
    if cnt_minus1 % 2 == 0:
    	return len(arr)
    else: 
    	newLeft = q[0] + 1
    	newRight = q[-1] - 1
    	print(newLeft, newRight)
    	return max((r-newLeft), (newRight-l))
    	
print(solve([-1,-1,-1,-1,-1, 1])) # 5
print(solve([-1,-1,1,1,-1])) # 4 
PreviousStrengthOfPasswordNextMove 0/1 InArray

Last updated 3 years ago

Was this helpful?

Very similar:

https://www.gyanblog.com/coding-interview/leetcode-solution-max-length-positive-product/