Google | Facebook | Phone Screen | 3 Solutions | Insert Node Into Sorted Circular List
Company: Meta (Facebook)
Role: e3, e4
Interview Stage: Phone Screen
Location: unknown
Year: unknown
Original Post Content:
Note: Hey There! Just Take A Look At The Code And You'll Get It.
Note: Time Complexity And Auxiliary Space Is Mentioned With The Code.
Approach 1 : Using Sorting + Buffer
class InsertUsingVector {
static bool comp(Node* node1, Node* node2) {
return node1->data < node2->data;
}
public:
// Method to insert a new node in the circular list, using sorting with vector - O(NLogN) & O(N)
Node* sortedInsertToList(Node* headNode, int key) {
if(!headNode)
return new Node(key);
vector<Node*> nodes;
nodes.push_back(new Node(key));
Node* currNode = headNode;
int sz = 1, seeingFirstNodeFirstTime = 1;
while(seeingFirstNodeFirstTime || currNode != headNode) {
nodes.push_back(currNode);
currNode = currNode->next;
seeingFirstNodeFirstTime = 0;
sz++;
}
sort(begin(nodes), end(nodes), comp);
for(int index = 1; index < sz; ++index) {
nodes[index - 1]->next = nodes[index];
}
nodes[sz - 1]->next = nodes[0];
return nodes[0];
}
};
Approach 2 : Using Dynamic Node Creation
class InsertByCreatingNewList {
public:
// Method to insert a new node in the circular list, using dynamic node creation - O(N) & O(N)
Node* sortedInsertToList(Node* headNode, int key)
if(!headNode)
return new Node(key);
Node* dummyHead = new Node(-1);
Node* dummyTail = dummyHead;
Node* currNode = headNode;
bool seeingFirstNodeFirstTime = true, isNodeInserted = false;
while(seeingFirstNodeFirstTime || currNode != headNode) {
if(!isNodeInserted && currNode->data > key) {
dummyTail->next = new Node(key);
dummyTail = dummyTail->next;
isNodeInserted = true;
}
dummyTail->next = new Node(currNode->data);
dummyTail = dummyTail->next;
currNode = currNode->next;
seeingFirstNodeFirstTime = false;
}
if(!isNodeInserted) {
dummyTail->next = new Node(key);
dummyTail = dummyTail->next;
}
dummyTail->next = dummyHead->next;
return dummyHead->next;
};
// Note: This solution contains memory leak problem as we're creating the new nodes dynamically but never freed
Approach 3 : Using Constant Auxiliary Space
class InsertWithoutCreatingList {
public:
// Method to insert a new node in the circular list, using constant auxiliary space - O(N) & O(1)
Node* sortedInsertToList(Node* headNode, int key) {
if(!headNode)
return new Node(key);
Node* dummyHead = new Node(-1);
Node* dummyTail = dummyHead;
Node* headNode = headNode;
Node* nextNode = nullptr;
bool seeingFirstNodeFirstTime = true, isNodeInserted = false;
while(seeingFirstNodeFirstTime || headNode != headNode) {
nextNode = headNode->next;
if(!isNodeInserted && headNode->data > key) {
dummyTail->next = new Node(key);
dummyTail = dummyTail->next;
isNodeInserted = true;
}
dummyTail->next = headNode;
dummyTail = dummyTail->next;
headNode = nextNode;
seeingFirstNodeFirstTime = false;
}
if(!isNodeInserted) {
dummyTail->next = new Node(key);
dummyTail = dummyTail->next;
}
dummyTail->next = dummyHead->next;
headNode = dummyHead->next;
delete dummyHead;
return headNode;
}
};
APROVE IF YOU LIKE THE SOLUTION 👍
Note: These solutions are created by myself after reading these posts :-
Link1 - https://leetcode.com/discuss/interview-question/435345/Google-or-Phone-or-Insert-into-a-Sorted-Circular-Linked-List
Link2 - https://leetcode.com/discuss/interview-question/429160/Facebook-or-Phone-Screen-or-Insert-into-a-Sorted-Circular-Linked-list