Uploaded January 2019 | Updated November 2026, 3 hours ago
Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe.com/pricing
πΉ Intuitive Video Explanations
π Run Code As You Learn
πΎ Save Progress
βNew Unseen Questions
π Get All Solutions
Question: You are given a standard linked list with next pointer BUT each node carries an additional random pointer to any given node in the linked list. Clone the linked list.
Let us first see the mental barriers and problems that we face while solving this.
It is trivial to make a copy of the linked list nodes with only the next pointers, but the random pointer on each node presents a problem.
We will notice that we have trouble establishing the connection between the original linked list and the random pointers between nodes in the cloned linked list.
Approach 1 (Linear Space Solution)
Use a hashtable to facilitate the cloning.
Complexities
Time: O( n )
We will perform linear time traversals that keep our asymptotic behavior linear.
Space: O( n )
We will store a clone mapping for each node entailing linear space complexity with respect to the original list passed to us.
Approach 2 (Constant Space Solution)
Use the original list's node's next pointer to map to the clone list.
Complexities
Time: O( n )
We are still only doing linear time traversals
Space: O( 1 )
We do not store any auxiliary space that will scale as the input size gets arbitrarily large.
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww
Tuschar Roy: youtube.com/user/tusharroy2525
GeeksForGeeks: youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ
Jarvis Johnson: youtube.com/user/VSympathyV
Success In Tech: youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw
++++++++++++++++++++++++++++++++++++++++++++++++++
This question on Leetcode: leetcode.com/problems/copy-list-with-random-pointer
Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe.com/pricing
πΉ Intuitive Video Explanations
π Run Code As You Learn
πΎ Save Progress
βNew Unseen Questions
π Get All Solutions
Question: You are given a standard linked list with next pointer BUT each node carries an additional random pointer to any given node in the linked list. Clone the linked list.
Let us first see the mental barriers and problems that we face while solving this.
It is trivial to make a copy of the linked list nodes with only the next pointers, but the random pointer on each node presents a problem.
We will notice that we have trouble establishing the connection between the original linked list and the random pointers between nodes in the cloned linked list.
Approach 1 (Linear Space Solution)
Use a hashtable to facilitate the cloning.
Complexities
Time: O( n )
We will perform linear time traversals that keep our asymptotic behavior linear.
Space: O( n )
We will store a clone mapping for each node entailing linear space complexity with respect to the original list passed to us.
Approach 2 (Constant Space Solution)
Use the original list's node's next pointer to map to the clone list.
Complexities
Time: O( n )
We are still only doing linear time traversals
Space: O( 1 )
We do not store any auxiliary space that will scale as the input size gets arbitrarily large.
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww
Tuschar Roy: youtube.com/user/tusharroy2525
GeeksForGeeks: youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ
Jarvis Johnson: youtube.com/user/VSympathyV
Success In Tech: youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw
++++++++++++++++++++++++++++++++++++++++++++++++++
This question on Leetcode: leetcode.com/problems/copy-list-with-random-pointer
![Total Occurrences Of K In A Sorted Array (Facebook Software Engineering Interview Question)
Try Our Full Platform: https://nas.io/backtobackswe
πΉ Intuitive Video Explanations
βNew Unseen Questions
π Get All Solutions
Question: Given a sorted array, find the total count of occurrences of a given value K as efficiently as possible. (Hint: The best answer runs in O(log(n)) time)
This was a question that I got in my 1st round Facebook interview. Error: At 3:27 the Answer should have said 1. Sorry.
Approach 1:
Linear scan and count
Time: O(N)
Space: O(1)
Remember: whenever an array is sorted it automatically unlocks binary search which is very fast. Binary search should be first on your mind and you should tell your interviewer so immediately.
Binary search operates off what is called an invariant. Our invariant is that no matter how often we split the search space, the space will stay sorted and hence again binary search can be used on it (doesnt have to be perfectly sorted though as we will see in other problems)
Approach 2:
Binary search for the first element and then linear scan to count the elements from that first element to its last occurrence.
Average Case
Time: O(log(n))
Space: O(1)
Worst Case
Time: O(N)
Space: O(1) [O(log(n) if the binary search is recursive]
This is the approach that I was stuck on, but my interviewer then said, Can we keep it log(n) in every case?
Then I knew that we would only be dealing with binary searches, any linear scans would be unnecessary
I knew I needed to get the count somehow so it occurred to me...if it is sorted, the lastOccurrence will give us the count if we just subtract the first occurrence from it and add 1. That is the total elements between the first and last occurrence and they all must be that number itself since the array is sorted
Approach 3:
Binary search for the first occurrence. If not found then return 0 occurrences total. If found, binary search for the 2nd element.
The answer is: lastOccurrence - firstOccurrence + 1
Time: O(log(n)) + O(log(n)) = O(log(n))
Space: O(1) [O(log(n)) if the binary search is recursive]
Takeaways:
-) If an array is sorted, immediately think binary search even if the problem is not a search problem (in this case it is)
-) Be vocal, your interviewer will guide you. Say approaches that may even be suboptimal, they may be close to the optimal result
+++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: https://www.youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww
Tuschar Roy: https://www.youtube.com/user/tusharroy2525
GeeksForGeeks: https://www.youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ
Jarvis Johnson: https://www.youtube.com/user/VSympathyV
Success In Tech: https://www.youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw
++++++++++++++++++++++++++++++++++++++++++++++++++ Total Occurrences Of K In A Sorted Array (Facebook Software Engineering Interview Question)](https://i.ytimg.com/vi/RlXtTF34nnE/mqdefault.jpg)









