Amazon SDE2 Online Assessment
Anonymous User
1495
  1. Given an array of integers(unsorted) and an integer K, count the number of sub-arrays whose difference between smallest and largest element is less than or equal to K

I solved it in O(n^2). Got TLE for 3/15 Test cases. Stuck with optimization.Do comment if you guys have any optimised approaches

  1. Minimum number of swaps(within the string, you can't flip the bits) required to convert a binary string to palndrome. If it's not possible, return -1

I used the following logic, but only partial test cases were passed. Do comment if you have alternate approaches

case 1: string length is even
If number of ones in the string is not even, return -1. Else, return numberOfOnes/2
case 2: string length is odd
If difference between number of ones before mid character and after mid character is even, return differenceInOnes/2.
Else, return (differenceInOnes/2)+1 (Note: You can always make a odd lengthed string palindrome by moving the extra one to middle)

I don't know what went wrong with my logic on problem 2.

Totally disappointed with my performance. Will strive hard and do well next time. Please post if you guys have any optimised/efficient solutions

Thanks

Comments (5)