Python interview with a LinkedIn engineer: Matching pairs

preview_player
Показать описание


Рекомендации по теме
Комментарии
Автор

The interviewer said he was glad he tested for duplicates because it would of been poor performance if he didnt test for that, but @1:33, he saids you can assume their would be no duplicates in the list.

brandondownhour
Автор

Here is my solution for this problem; No need for second loop:

def find_pair(values, target):
value_dict = {}
for value in values:
if (target - value) in value_dict:
return "{} and {}".format(target - value, value)
value_dict[value] = value
return "No Valid Pair"

cafafans
Автор

If only real job interviews were this easy. It's always "code a solution to the cure for cancer with O(log n)

preposterousrhinocerous
Автор

A slow, but space efficient solution

Def find_pair_sum(values, sum)
For i in range(len(values)):
For j in range(i, len(values)):
If (values[i] + values[j] == sum):
Return values[i], values[j]
Return None

Speed is of triangle numbers of N - 1, which is similar to (N(N-3))/2 at the longest, but only uses a few bytes of ram, for i and j. Not really proportional to N.

boggless
Автор

def find_pairs(values, target):
for num, x in enumerate(values):
if x > target:
pass
else:
solution = target - x
if solution in values[num+1:]:
return str(solution) + '&' + str(x)
return ('No pairs')

jordondyer
Автор

best solution :
1. use set (because the question didnt ask for the index, else you need to use a hashmap)
2. complete it in one iteration - O(n) by checking whether the complement exists in the set then and there.
java solution:
static int[] getMatchingPairs(int[] nums, int target){
HashSet<Integer> set = new HashSet<>();
for(int num : nums){
int complement = target - num;
if(set.contains(complement)) return new int[] {complement, num};
set.add(num);
}
return new int[] {-1, -1};
}

xskrish
Автор

t=[14, 13, 6, 7, 8, 10, 1, 2]
target=3
cnt=0

for i in t:
if (i < target and cnt==0):
#find the pair that equals target
for pair in t:
tot=i + pair
if tot == target:
Print i, pair
cnt +=1
else:
break

waterskicompany
Автор

Still conducting it in Python 2 I see! Can't say how pleased I am that we decided to move to 3.x sooner rather than later. So many interesting quirks with 2 but don't think it would be right for us to make any tutorials other than for 3 anymore!

Seeing xrange() always reminds me of Raymond Hettinger's account of how he improved Python by putting generators anywhere he could. Great guy and would definitely recommend any of his keynotes for inspiration for those preparing for any Python-heavy roles.

LivePython
Автор

def pairs(nums, target):
for num in range(len(nums)):
if (target-nums[num]) in nums[num+1:]:
return(str(nums[num])+ " and " + str((target-nums[num])))
return("can't be done")

tech_in_three
Автор

@13.55 that excitement when 'interviewer' finally 'understands' that -2 and says "its because 0 - 2 = -2, True is 0" :(, man please in what language True is 0 man its 1 - 3

aberkb
Автор

Most optimal solution: 
A single pass through the dataset, using a hash map. For each value, you check the compliment of the value against the target. If it exists in the hash map (O(1) lookup), you have you two values, if it doesn't exist in the hash map, add it to the hash map and move on to the next value.

def findMatch(self, dataset, target):
map = {}
for i, n in enumerate(dataset):
m = target - n
if m in map:
return [map[m], i]
else:
 map[n] = i

stardreamse
Автор

The code could be really neat and short for this approach 😊

nikhilchawla
Автор

def getpairs(values, target):
for i in values:
target_diff = target - i
for j in values:
if target_diff == j:
return str(i) + " and " + str(j)
return "No valid pairs."

tokleezy
Автор

Code for this question:

x=[12, 14, 8, 6, 1, 2]
y=x
for i in x:
for j in y:
if i+j==3:
result=f"{i}, {j}"
break
print(result)

Arjun-vydx
Автор

Just one for loop needed and I guess it'd be more efficient to exclude values > target.


def find_pair(values, target):
val_dict = {}
for val in values:
if val < target:
if val_dict.get(val) in values:
print(f"pairs found {val} {val_dict[val]}")
return 0
val_dict[val] = target - val
print("None found")
return 1

abhisheknair
Автор

Tried this out


def check_sum(list_values, target_sum):
rem=list(map(lambda x:target_sum-x, list_values))
for i, x in enumerate(rem):
if x in list_values:
print("This two values are complimentary {} and {}".format(x, list_values[i]))

iankoome
Автор

waiting for the day when interviewers asks me such questions lol

sagarshah
Автор

def find_pair(number_array, targeted_sum):
for i in range(len(number_array) - 1):
for j in range(i + 1, len(number_array)):
if number_array[i] >= targeted_sum or number_array[j] >= targeted_sum:
continue
if (number_array[i] + number_array[j]) == targeted_sum:
return '%d and %d' % (number_array[i], number_array[j])
return 'No possible'

hoangnguyenvuhuy
Автор

I haven't gone through the whole video but thought of this solution.

def find_pairs(values, target):
pairs = {}
for index, x in enumerate(values):
if x not in pairs:
pairs[target-x] = x
else:
return [pairs[x], x]
return 'No Valid Pairs'

- Pairs will store (target-number) as the key and number of number as the value
Rest is easy to figure out.

krishnanigalye
Автор

This interviewer needs to stop eating...

mattyh