Alibaba telephone interview-Alibaba interview to solve two conflicts

Source: Internet
Author: User

Alibaba telephone interview-Alibaba interview to solve two conflicts

Situation: Party A and Party B handle some matters, each of which has a start time and a deadline. However, the agent issues of Party A and Party B may conflict. If there are other agent issues in a code issue, it is considered as a conflict. It is required to merge the agent items of Party A and Party B. There cannot be conflicts.

Solution:

I often use java to simulate this problem.

Define the event handling:

public class Node{private int startTime;private int endTime;Node cNode;}
The agent items of Party A and Party B are already ordered linked lists.

Initialize agent:

Node nodeA = new Node (1, 2); // initialize Node AnodeA. addNode (new Node (2, 6); nodeA. addNode (new Node (3, 7); nodeA. addNode (new Node (4, 9); nodeA. addNode (new Node (7, 12); nodeA. addNode (new Node (16, 19 ));

Initialize the agent of Party B:

Node nodeB = new Node(2, 4);nodeB.addNode(new Node(3, 6));nodeB.addNode(new Node(7, 10));nodeB.addNode(new Node(9, 15));nodeB.addNode(new Node(18, 20));
Note: There may be conflicts between the agent and the agent after the merger.

Based on this, we will directly merge the agent items of Party A and Party B, and then delete the conflicting items from the agent items after the merger.

PrintNode (nodeA); printNode (nodeB); Node mergeNode = mergeWithNoSort (nodeA, nodeB); // merge two nodes into printNode (mergeNode); Node deleteNode = deleteNode (mergeNode ); // Delete the conflicting node printNode (deleteNode );
Running result

Merge two nodes:

Public static Node mergeWithNoSort (Node nodeA, Node nodeB) {Node tempNode = nodeA; // merge nodeA and nodeB // USE insert and delete to insert nodeB into nodeA Node insertNode = nodeB; while (tempNode! = Null) {if (tempNode. cNode = null) {if (insertNode. startTime <= tempNode. startTime) {Node tempInsert = insertNode; insertNode. cNode = tempNode; tempNode = insertNode; insertNode = tempInsert; break;} else {tempNode. cNode = insertNode; return nodeA;} else {if (insertNode. startTime <= tempNode. startTime) {Node tempInsert = insertNode; insertNode. cNode = tempNode; tempNode = insertNode; insertNode = tempInsert; break;} else if (insertNode. startTime> tempNode. startTime & insertNode. startTime <= tempNode. cNode. startTime) {Node tempInsert = insertNode. cNode; insertNode. cNode = tempNode. cNode; tempNode. cNode = insertNode; // The clever use of nodeB is already in order. Write less the cycle insertNode = tempInsert;} else {tempNode = tempNode. cNode; }}return nodeA ;}

Function for deleting duplicate nodes:

// Delete the public static Node deleteNode (Node node) {Node tempNode = node; while (tempNode! = Null & tempNode. cNode! = Null) {if (tempNode. cNode. startTime <tempNode. endTime) // if a conflict exists, delete the Node {Node temp = tempNode. cNode; tempNode. cNode = temp. cNode; continue;} tempNode = tempNode. cNode;} return node ;}


Print node functions:

// Print a public static void printNode (Node node) {Node tempNode = node; while (tempNode! = Null) {System. out. print ("(" + tempNode. startTime + "," + tempNode. endTime + ")->"); tempNode = tempNode. cNode;} System. out. println ();}
Add a node to the last node of the node:

// Add a public void addNode (Node node) {Node tempNode = this; while (tempNode. cNode! = Null) {tempNode = tempNode. cNode;} tempNode. cNode = node ;}
In fact, it is not too difficult logically to analyze it carefully. For a person who hasn't written algorithms for a long time, when the interview is successful, the interview is rejected because of the tragedy. It seems that before looking for a job, you must write more code, review the data structure more.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.