Skip to content

[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 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
);
}
}
}
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;
}
}
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`.