Facebook Interview Question

Got the following question:

You have two lists:
user_id 1, user_id 2, timestamp
accepted=([1,2,"2020-01-01"],[3,2,"2020-01-02"],[2,1,"2020-02-02"],...)
removed=([2,1,"2020-01-03"],[2,3,"2020-01-05"],[2,1,"2020-04-02"]..)

The accepted list is the friendships which were accepted, while removed are the opposite. Create a new list which outputs user_ids with their accepted and removed friendship date, e.g. the following output:

(1,2,"2020-01-01","2020-01-03")
(3,2,"2020-01-02","2020-01-05")
(1,2,"2020-02-02","2020-04-02")

Do not output groups which are still friends

I was able to output the first two rows but couldn't work my way around duplicate instances - is there a similar leetcode question?

Edit: this is my bruteforce solution - not the most efficient, open to feedback:

def friendgroup(a,b): #a is accepted list , b is removed
  h={}
  r={}
  l=[]
  
  for i in a:
    j=i
    friends=str(sorted([j[0],j[1]]))
    if friends in h:
      h[friends].append(j[2])

    else:
      h[friends]=[j[2]]

  for k in b:
    j=k
    friends=str(sorted([j[0],j[1]]))
    if friends in r:
      r[friends].append(j[2])

    else:
      r[friends]=[j[2]]

  for p in r:
    if p in h:
      k=sorted(r[p]) # to make sure dates are in a chronological order
      #print(k)
      o=sorted(h[p]) # to make sure dates are in a chronological order
      for i in range(len(k)): # only run to the number of times of a removal
        w=str([p,o[i],k[i]])
        w=w.replace("[","")
        w=w.replace("]","")
        
        l.append([w])
        

  return l
Comments (7)