- 
 
- 
  Notifications
 You must be signed in to change notification settings 
- Fork 0
[Hacker Rank] Interview Preparation Kit: Linked Lists: Find Merge Poi... #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
 
  Merged
 
 
 
 
  Merged
 Changes from all commits
 Commits
 
 
 File filter
Filter by extension
Conversations
 Failed to load comments. 
 
 
 
  Loading
 
 Jump to
 
 Jump to file
 
 
 
 Failed to load files. 
 
 
 
  Loading
 
 Diff view
Diff view
There are no files selected for viewing
 
 
 
 92 changes: 92 additions & 0 deletions
 
 
 
 ...es-csharp-test/src/hackerrank/interview_preparation_kit/linked_list/FindMergeNode.Test.cs
 
 
 
 
  
 
 
 
 
 
 
 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
 Learn more about bidirectional Unicode characters
 
 
 
 
 | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit; | ||
|  | ||
| [TestClass] | ||
| public class FindMergeNodeTest | ||
| { | ||
| class FindMergeNodeTestCase() | ||
| { | ||
| public string title = ""; | ||
| public LinkedList.Node llist1 = new(0); | ||
| public LinkedList.Node llist2 = new(0); | ||
| public int expected; | ||
| } | ||
|  | ||
| private static FindMergeNodeTestCase[] tests = []; | ||
|  | ||
| [TestInitialize] | ||
| public void testInitialize() | ||
| { | ||
| // Test Case 0 | ||
| // | ||
| // 1 | ||
| // \ | ||
| // 2--->3--->NULL | ||
| // / | ||
| // 1 | ||
| // | ||
| LinkedList.Node tc0_ll1_1 = new(1); | ||
| LinkedList.Node tc0_ll1_2 = new(2); | ||
| LinkedList.Node tc0_ll1_3 = new(3); | ||
| tc0_ll1_1.next = tc0_ll1_2; | ||
| tc0_ll1_2.next = tc0_ll1_3; | ||
|  | ||
| LinkedList.Node tc0_ll2_1 = new(1); | ||
| tc0_ll2_1.next = tc0_ll1_2; | ||
|  | ||
| // Test Case 1 | ||
| // | ||
| // 1--->2 | ||
| // \ | ||
| // 3--->Null | ||
| // / | ||
| // 1 | ||
| // | ||
| LinkedList.Node tc1_ll1_1 = new(1); | ||
| LinkedList.Node tc1_ll1_2 = new(2); | ||
| LinkedList.Node tc1_ll1_3 = new(3); | ||
| tc1_ll1_1.next = tc1_ll1_2; | ||
| tc1_ll1_2.next = tc1_ll1_3; | ||
|  | ||
| LinkedList.Node tc1_ll2_1 = new(1); | ||
| tc1_ll2_1.next = tc1_ll1_3; | ||
|  | ||
| tests = [ | ||
| new() | ||
| { | ||
| title = "Sample Test case 0", | ||
| llist1 = tc0_ll1_1, | ||
| llist2 = tc0_ll2_1, | ||
| expected = 2 | ||
| }, | ||
| new() | ||
| { | ||
| title = "Sample Test case 1", | ||
| llist1 = tc1_ll1_1, | ||
| llist2 = tc1_ll2_1, | ||
| expected = 3 | ||
| } | ||
| ]; | ||
| } | ||
|  | ||
| [TestMethod] | ||
| public void testLinkedListCycle() | ||
| { | ||
| int? result; | ||
|  | ||
| foreach (FindMergeNodeTestCase test in tests) | ||
| { | ||
| result = FindMergeNode.findMergeNode(test.llist1, test.llist2); | ||
| Assert.AreEqual( | ||
| test.expected, | ||
| result, | ||
| String.Format( | ||
| "{0} findMergeNode({1}, {2}) => must be: {3}", | ||
| test.title, | ||
| test.llist1, | ||
| test.llist2, | ||
| test.expected | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | 
 
 
 
 39 changes: 39 additions & 0 deletions
 
 
 
 ...hm-exercises-csharp/src/hackerrank/interview_preparation_kit/linked_list/FindMergeNode.cs
 
 
 
 
  
 
 
 
 
 
 
 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
 Learn more about bidirectional Unicode characters
 
 
 
 
 | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // @link Problem definition [[docs/hackerrank/interview_preparation_kit/linked_lists/find-the-merge-point-of-two-joined-linked-lists.md]] | ||
|  | ||
| namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit; | ||
|  | ||
| using System.Diagnostics.CodeAnalysis; | ||
|  | ||
| public class FindMergeNode | ||
| { | ||
| [ExcludeFromCodeCoverage] | ||
| protected FindMergeNode() { } | ||
|  | ||
| public static int? findMergeNode(LinkedList.Node head1, LinkedList.Node head2) | ||
| { | ||
| List<LinkedList.Node> llindex = []; | ||
| LinkedList.Node? node = head1; | ||
| int? result = null; | ||
|  | ||
| while (node != null) | ||
| { | ||
| llindex.Add(node); | ||
| node = node.next; | ||
| } | ||
|  | ||
| node = head2; | ||
| bool resume = true; | ||
| while (node != null && resume) | ||
| { | ||
| if (llindex.Contains(node)) | ||
| { | ||
| result = node.data; | ||
| resume = false; | ||
| } | ||
| llindex.Add(node); | ||
| node = node.next; | ||
| } | ||
|  | ||
| return result; | ||
| } | ||
| } | 
 
 
 
 103 changes: 103 additions & 0 deletions
 
 
 
 ...preparation_kit/linked_lists/find-the-merge-point-of-two-joined-linked-lists.md
 
 
 
 
  
 
 
 
 
 
 
 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
 Learn more about bidirectional Unicode characters
 
 
 
 
 | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # [Linked Lists: Find Merge Point of Two Lists](https://www.hackerrank.com/challenges/find-the-merge-point-of-two-joined-linked-lists) | ||
|  | ||
| - Difficulty: `#easy` | ||
| - Category: `#ProblemSolvingIntermediate` | ||
|  | ||
| Given pointers to the head nodes of linked lists that merge together | ||
| at some point, find the node where the two lists merge. | ||
| The merge point is where both lists point to the same node, | ||
| i.e. they reference the same memory location. | ||
| It is guaranteed that the two head nodes will be different, | ||
| and neither will be NULL. | ||
| If the lists share a common node, return that node's value. | ||
|  | ||
| **Note**: After the merge point, both lists will share the same node pointers. | ||
|  | ||
| ## Example | ||
|  | ||
| In the diagram below, the two lists converge at Node x: | ||
|  | ||
| ```text | ||
| [List #1] a--->b--->c | ||
| \ | ||
| x--->y--->z--->NULL | ||
| / | ||
| [List #2] p--->q | ||
| ``` | ||
|  | ||
| ## Function Description | ||
|  | ||
| Complete the findMergeNode function in the editor below. | ||
|  | ||
| findMergeNode has the following parameters: | ||
|  | ||
| - SinglyLinkedListNode pointer head1: a reference to the head of the first list | ||
| - SinglyLinkedListNode pointer head2: a reference to the head of the second list | ||
|  | ||
| ## Returns | ||
|  | ||
| - int: the `data` value of the node where the lists merge | ||
|  | ||
| ## Input Format | ||
|  | ||
| Do not read any input from stdin/console. | ||
|  | ||
| The first line contains an integer `t`, the number of test cases. | ||
|  | ||
| Each of the test cases is in the following format: | ||
| The first line contains an integer, `index`, | ||
| the node number where the merge will occur. | ||
|  | ||
| The next line contains an integer, | ||
| `list1count` that is the number of nodes in the first list. | ||
| Each of the following `list1count` lines contains a `data` value for a node. | ||
| The next line contains an integer, | ||
| `list2count` that is the number of nodes in the second list. | ||
| Each of the following lines contains a `data` value for a node. | ||
|  | ||
| ## Constraints | ||
|  | ||
| The lists will merge. | ||
|  | ||
| `head1, head2 != null` | ||
|  | ||
| `head1 != head2` | ||
|  | ||
| ## Sample Input | ||
|  | ||
| The diagrams below are graphical representations of the | ||
| lists that input nodes `head1` and `head2` are connected to. | ||
|  | ||
| ## Test Case 0 | ||
|  | ||
| ```text | ||
| 1 | ||
| \ | ||
| 2--->3--->NULL | ||
| / | ||
| 1 | ||
| ``` | ||
|  | ||
| ## Test Case 1 | ||
|  | ||
| ```text | ||
| 1--->2 | ||
| \ | ||
| 3--->Null | ||
| / | ||
| 1 | ||
| ``` | ||
|  | ||
| ## Sample Output | ||
|  | ||
| ```text | ||
| 2 | ||
| 3 | ||
| ``` | ||
|  | ||
| ## Explanation | ||
|  | ||
| Test Case 0: As demonstrated in the diagram above, | ||
| the merge node's data field contains the integer `2`. | ||
| Test Case 1: As demonstrated in the diagram above, | ||
| the merge node's data field contains the integer `3`. | 
 Add this suggestion to a batch that can be applied as a single commit.
 This suggestion is invalid because no changes were made to the code.
 Suggestions cannot be applied while the pull request is closed.
 Suggestions cannot be applied while viewing a subset of changes.
 Only one suggestion per line can be applied in a batch.
 Add this suggestion to a batch that can be applied as a single commit.
 Applying suggestions on deleted lines is not supported.
 You must change the existing code in this line in order to create a valid suggestion.
 Outdated suggestions cannot be applied.
 This suggestion has been applied or marked resolved.
 Suggestions cannot be applied from pending reviews.
 Suggestions cannot be applied on multi-line comments.
 Suggestions cannot be applied while the pull request is queued to merge.
 Suggestion cannot be applied right now. Please check back later.