Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,120 views
in Interview-Experiences by Expert (30,360 points) | 1,120 views

2 Answers

0 like 0 dislike
Best answer
Hi.. this is the problem for the Phone Interview in Uber

A jump means adding or subtracting 1.
“Converge” means all the elements get to the same value.
Given an integer array, find if all the elements could converge with 1 jump (or less) and return the converge value.

input elements could be numbers between 0 and 9 inclusive
9 plus 1, gives you 0
0 minus 1, gives you 9

Test Case
[ 1, 0, 9, 9, 1, 1, 0 , 3]
Output = -1

As if you jump down from 1 to 0 on all 1s, and you jump up on all 9 to 0, all numbers are going to converge into 0 value. So your output is the converge value, 0 in this case. Return -1 if there is no convergence like would be the case if in the example we add a “3” at the end (or any other number).
by Expert (30,360 points)
0 like 0 dislike
arr=sorted(set(arr))   
for _ in range(len(arr)):
    if arr[-1]-arr[0]<=2:
        return (arr[-1]+arr[0])//2%10         
    arr=arr[1:]+[arr[0]+10]
return -1
by Expert (30,360 points)