Twitter Phone interview
Anonymous User
2073

Q1) Median from Data Stream - https://leetcode.com/problems/find-median-from-data-stream/
Discussed all approaches - Brute Force, Heap, BST with time and space complexity.
Followup - Stream has trillions of numbers
(Discussed range the numbers are in and then proposed frequency array approach)

Q2) Given a table "FollowerRelationship" with two columns - Follower Followee
1 ->2
2 ->3
1 -> 4
3 -> 2
Identify all the pairs such that A follows B and B also follows A.
Followup: If this data is not huge and can fit in memory, what approach would you take to identify these pairs ?

For first part, I did inner join with same table. Mentioned this is the initial approach and before I could do any optimization, the interviewer moved on to followup. (Any better ideas please suggest)

For followup, I used HashSets - Hash a pair, if the reverse of pair already exists in Set, add this pair to result. The interviewer asked what if we use HashMaps. I mentioned that we could take Key as Follower and Value as Followee. For a given key, all values can be stored as a LinkedList (Linear Chaining ) or BST (LogN to search). In Hashset we could check if reverse of pair exists in O(1) but in HashMap it would depend on the kind of implementation, but would have higher complexity compared to HashSet.

**Please suggest if there are any better approaches to Q2 **

Comments (7)