Python interview with a LinkedIn engineer: Matching pairs

    3
    42



    Book a mock interview or coaching session with a LinkedIn engineer as early as tomorrow on interviewing.io! Sign up here: https://interviewing.io/signup

    Check out the feedback by the LinkedIn interviewer and the full transcript on https://interviewing.io/recordings/Python-LinkedIn-2

    Or view other Python interviews: https://interviewing.io/python-interview-questions

    Disclaimer: All interviews are shared with explicit permission from the interviewer and the interviewee, and all interviews are anonymous. interviewing.io has the sole right to distribute this content.

    source

    Previous articleREAL DEVOPS TECHNICAL INTERVIEW REQUIREMENTS – Nginx, Docker, Terraform, AWS, Ansible, and Jenkins
    Next articleWhich Kubernetes certification is good for you?||#CKA||#CKAD

    42 COMMENTS

    1. Rather confusing interview. We write python to literally provide micro services in numerous docker containers reading/writing to Qs/Topics/S3 buckets/Lambdas/FTP servers/RSDBs/plotting graphs and you spend this entire interview on find the sum of two numbers in a list?? This is a bash interview! If this was my interview I would fail it. If you asked me to write 1000 lines to contact a server reliably and download a terabyte of data, I am good.

    2. c = [14,13,6,7,8,10,1,2]

      from itertools import combinations

      def comb(a,target):

      solution = 'No valid pairs'

      comb = combinations(a,2)

      for i in list(comb):

      if sum(i) == target:

      solution = '{0} and {1}'.format(i[0],i[1])

      print(solution)

      comb(c,27)

    3. #No need of dictionary. We can sort the list and iterate
      #may be add one more step is to remove numbers that are already >=target from the list
      def findPair(l,target):
      l.sort()
      for i,item in enumerate(l):
      for j in range(i,len(l)):
      if item + l[j] == target:
      print (item,l[j])

      l=[6,8,12,9,17,10,45]
      target=21
      findPair(l,target)

    4. my solution
      num_list = [14, 13, 6, 7, 8, 10, 1, 2]
      target = 3
      num_set = []

      filtered_num_list = [i for i in num_list if i <= target]

      for num in filtered_num_list:
          for n in filtered_num_list[1:]:
              if num + n == target:
                  if (num, n) not in num_set and (n, num) not in num_set:
                      num_set.append((num, n))

    5. 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

    6. The criticism in the comments are ridiculous. The whole purpose of this is to practice, learn, get better, and gather feedback. This is why young software developers have a hard time because of the rude personalities that's in the area. Golden rule is to treat others the way you want to be treated. Let's not act like day one geniuses and have never improved in this line of work. smh

    7. 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"

    8. It's so much more easy to do with a set and you only need to loop once.

      Define the complement of a number to be the number that you need to add to it to get the target,

      Loop through the list and see if the set contains the complement of the current number, if not add the current number to the set. Return the numbers if find the complement and false if you finish the loop without.

    9. def findpairs(n,m):
      m=int(m)
      start=0
      end=0
      n.sort()
      matchfound=False
      if len(n) <=1 :
      return "No valid pairs"
      for i in range(len(n)):
      for j in range(len(n)):
      if j!=i :
      if n[i]+n[j]==m :#match found
      start=n[i]
      end=n[j]
      matchfound=True
      break
      if matchfound:
      break
      if matchfound:
      return str(start) + " and " + str(end)
      else:
      return "No valid pairs"

    10. def find_pairs(arr, target):

      d = {arr[i] : False for i in range(len(arr))}

      ans = []

      for num in arr:

      reminder = target – num

      if d[num] == False and reminder in d and num and num != reminder:

      ans.append(f"{num} and {reminder}")

      d[num] = True

      d[target – num] = True

      return ans if ans else "no valid pairs."

    11. my solution:

      target_num = 9 # change target_num to whatever you want
      list = [1, 10, 4, 5] # change numbers in list to whatever you want

      def find_pair(temp, ind1):
      ind2 = 0
      for num in list:
      if ind2 != ind1:
      if num + temp == target_num:
      print(f"#'s that add up to {target_num}: ( {temp}, {num} ) ")
      return True
      ind2 += 1

      ind = 0
      for num_ in list:
      if find_pair(num_, ind) == True:
      break
      elif ind == len(list)-1:
      print(f"No pairs add up to {target_num}");
      ind += 1

    12. the solution he found is extremely complicated and unecessary, this can be easily done with a double for interation, and then testing the sum of the numbers: for i in values:

      for j in values:

      if i+j==target:

      list1.append(i)

      list1.append(j)

      a=sorted(set(list1))

      list2.append(a)

      list1.clear()

      then wth a litlle tupple and sets adjustment, you can easily extract the numbers, without duplicates, i don;t understand why the long complicated coding, programers need to be simple and efficient.

    13. how about this (nice, simple and short):
      # Program to give valid pairs which add up to target

      def get_pairs(nums, target):
      sublist = list(filter(lambda x: x <= target, sorted(nums)))
      for i in sublist:
      for j in sublist:
      if i + j == target:
      return((i, j))

      return 'No valid pairs'

      nums = [14, 13, 6, 7, 8, 10, 1, 2]
      target = 24
      pairs = get_pairs(nums, target)
      print(pairs)

    14. 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.