public void BasicTest()
        {
            // Single duplicate
            var list = ListHelpers.CreateLinkedList(1, 2, 2, 3);

            ValidateResult(list, 1, 2, 3);

            // No duplicates
            list = ListHelpers.CreateLinkedList(1, 2, 3);
            ValidateResult(list, 1, 2, 3);

            // Three of the same values in the row
            list = ListHelpers.CreateLinkedList(1, 2, 2, 2, 3);
            ValidateResult(list, 1, 2, 3);

            // Duplicates not next to each other
            list = ListHelpers.CreateLinkedList(1, 2, 3, 2);
            ValidateResult(list, 1, 2, 3);

            // Duplicates next to each other with a third later in the list
            list = ListHelpers.CreateLinkedList(1, 2, 2, 3, 2);
            ValidateResult(list, 1, 2, 3);

            // Several duplicates throughout the list
            list = ListHelpers.CreateLinkedList(1, 2, 2, 3, 2, 4, 3, 5, 5, 6);
            ValidateResult(list, 1, 2, 3, 4, 5, 6);
        }
        public void BasicTest()
        {
            var list1        = ListHelpers.CreateLinkedList(1, 2, 3);
            var list2        = ListHelpers.CreateLinkedList(4, 5, 6);
            var intersection = ListHelpers.CreateLinkedList(7, 8, 9);

            AddIntersection(list1, list2, intersection);
            Validate(list1, list2, intersection);

            // Different sized lists
            list1        = ListHelpers.CreateLinkedList(1);
            list2        = ListHelpers.CreateLinkedList(2, 3);
            intersection = ListHelpers.CreateLinkedList(4, 5, 6);
            AddIntersection(list1, list2, intersection);
            Validate(list1, list2, intersection);

            // Intersection is last node
            list1        = ListHelpers.CreateLinkedList(1, 2);
            list2        = ListHelpers.CreateLinkedList(3, 4, 5);
            intersection = ListHelpers.CreateLinkedList(6);
            AddIntersection(list1, list2, intersection);
            Validate(list1, list2, intersection);

            // Repeated values (to ensure references are being computed)
            list1        = ListHelpers.CreateLinkedList(1, 3);
            list2        = ListHelpers.CreateLinkedList(2, 2, 2);
            intersection = ListHelpers.CreateLinkedList(2);
            AddIntersection(list1, list2, intersection);
            Validate(list1, list2, intersection);

            // No intersection
            list1 = ListHelpers.CreateLinkedList(1, 2, 3);
            list2 = ListHelpers.CreateLinkedList(4, 5, 6);
            Validate(list1, list2, null);
        }
        private static void ValidateResult <T>(T x, Node <T> list, params T[] expectedResult)
            where T : IEquatable <T>, IComparable <T>
        {
            var result = Question2_4.PartitionList(list, x);

            ListHelpers.ValidateLinkedListContent(result, expectedResult);
        }
Example #4
0
        private static Node <T> AddLoop <T>(Node <T> head, params T[] loop)
            where T : IEquatable <T>
        {
            while (head?.Next != null)
            {
                head = head.Next;
            }

            var loopStart = ListHelpers.CreateLinkedList(loop);
            var loopEnd   = loopStart;

            while (loopEnd.Next != null)
            {
                loopEnd = loopEnd.Next;
            }

            if (head != null)
            {
                head.Next = loopStart;
            }

            loopEnd.Next = loopStart;

            return(loopStart);
        }
        private static void ValidateNotReversedHelper(int num1, int num2, int expectedResult)
        {
            var expectedResultArray = CreateList(expectedResult);
            var list1  = CreateDigitList(num1);
            var list2  = CreateDigitList(num2);
            var result = Question2_5.AddNotReversed(list1, list2);

            ListHelpers.ValidateLinkedListContent(result, expectedResultArray.Select(r => new Digit(r)).ToArray());
        }
        private static void ValidateResult <T>(Node <T> input, params T[] expectedResult)
            where T : IEquatable <T>
        {
            var input1 = ListHelpers.CloneList(input);
            var input2 = ListHelpers.CloneList(input);

            Question2_1.RemoveDuplicates(input1);
            Question2_1.RemoveDuplicatesNoSpace(input2);

            ListHelpers.ValidateLinkedListContent(input1, expectedResult);
            ListHelpers.ValidateLinkedListContent(input2, expectedResult);
        }
        public void InvalidInputsTest()
        {
            // Null input
            TestHelpers.AssertExceptionThrown(() => { Question2_2.FindKthToLast <int>(null, 0); }, typeof(ArgumentNullException));

            // K invalid
            var list = ListHelpers.CreateLinkedList(1, 2, 3);

            TestHelpers.AssertExceptionThrown(() => { Question2_2.FindKthToLast(list, -1); }, typeof(ArgumentOutOfRangeException));

            list = ListHelpers.CreateLinkedList(1, 2, 3);
            TestHelpers.AssertExceptionThrown(() => { Question2_2.FindKthToLast(list, 4); }, typeof(ArgumentOutOfRangeException));
        }
        public void EdgeCasesTest()
        {
            // One node
            var list = ListHelpers.CreateLinkedList(1);

            ValidateResult(0, list, 1);

            list = ListHelpers.CreateLinkedList(1);
            ValidateResult(1, list, 1);

            list = ListHelpers.CreateLinkedList(1);
            ValidateResult(2, list, 1);
        }
        public void BasicTest()
        {
            const int NumElements = 5;

            var list = ListHelpers.CreateLinkedList(Enumerable.Range(0, NumElements).Reverse().ToArray());

            for (int i = 0; i < NumElements; i++)
            {
                var input  = ListHelpers.CloneList(list);
                var result = Question2_2.FindKthToLast(input, i);

                Assert.AreEqual(i, result.Data);
            }
        }
        public void EdgeCasesTest()
        {
            // First item duplicated
            var list = ListHelpers.CreateLinkedList(1, 1, 2, 3);

            ValidateResult(list, 1, 2, 3);

            // Last item duplciated
            list = ListHelpers.CreateLinkedList(1, 2, 3, 3);
            ValidateResult(list, 1, 2, 3);

            // List with only one element
            list = ListHelpers.CreateLinkedList(1);
            ValidateResult(list, 1);
        }
        public void BasicTest()
        {
            // Add an element
            Node <int> head = new Node <int>(1);

            ListHelpers.ValidateLinkedListContent(head, 1);

            // Add two more elements
            head.AppendToTail(2);
            head.AppendToTail(3);
            ListHelpers.ValidateLinkedListContent(head, 1, 2, 3);

            // Remove the middle element
            head = head.DeleteNode(head, 2);
            ListHelpers.ValidateLinkedListContent(head, 1, 3);
        }
Example #12
0
        public void BasicTest()
        {
            // Test case given in the problem
            var demoList = ListHelpers.CreateLinkedList('a', 'b', 'c', 'd', 'e');

            ValidateResult(demoList, demoList.Next.Next, 'a', 'b', 'd', 'e');

            // Remove first node
            var list = ListHelpers.CreateLinkedList(1, 2, 3);

            ValidateResult(list, list, 2, 3);

            // Remove middle node
            list = ListHelpers.CreateLinkedList(1, 2, 3);
            ValidateResult(list, list.Next, 1, 3);
        }
Example #13
0
        public void BasicTest()
        {
            var list      = ListHelpers.CreateLinkedList(1, 2, 3);
            var loopStart = AddLoop(list, 4, 5, 6);

            Validate(list, loopStart);

            // Loop of size one
            list      = ListHelpers.CreateLinkedList(1, 2, 3);
            loopStart = AddLoop(list, 4);
            Validate(list, loopStart);

            // Loop is first item in the list
            loopStart = AddLoop(null, 1, 2, 3);
            Validate(loopStart, loopStart);
        }
        public void BasicTest()
        {
            var list = ListHelpers.CreateLinkedList(3, 2, 1);

            ValidateResult(2, list, 1, 3, 2);

            // No change needed (x is smaller)
            list = ListHelpers.CreateLinkedList(1, 2, 3);
            ValidateResult(0, list, 1, 2, 3);

            // No change needed (x is larger)
            list = ListHelpers.CreateLinkedList(1, 2, 3);
            ValidateResult(4, list, 1, 2, 3);

            list = ListHelpers.CreateLinkedList(1, 3, 3, 1);
            ValidateResult(2, list, 1, 1, 3, 3);
        }
        public void RemoveTests()
        {
            // Add three elements
            Node <int> head = new Node <int>(1);

            head.AppendToTail(2);
            head.AppendToTail(3);
            ListHelpers.ValidateLinkedListContent(head, 1, 2, 3);

            // Remove the middle one
            head = head.DeleteNode(head, 2);
            ListHelpers.ValidateLinkedListContent(head, 1, 3);

            // Remove the first one
            head = head.DeleteNode(head, 1);
            ListHelpers.ValidateLinkedListContent(head, 3);

            // Remove non-existent head
            head = head.DeleteNode(head, 4);
            ListHelpers.ValidateLinkedListContent(head, 3);
        }
Example #16
0
        public void BasicTest()
        {
            // One node
            var root   = new BinaryTreeNode <int>(0);
            var result = new List <List <int> >
            {
                ListHelpers.CreateList(0)
            };

            ValidateResult(result, root);

            // One node with children
            root   = TreeHelpers.CreateBinaryTree(0, 1, 2);
            result = new List <List <int> >
            {
                ListHelpers.CreateList(0),
                ListHelpers.CreateList(1, 2)
            };
            ValidateResult(result, root);

            // More complex tree
            var tmp1 = TreeHelpers.CreateBinaryTree(4, 5, 6);
            var tmp2 = TreeHelpers.CreateBinaryTree(7, 8, 9);
            var tmp3 = TreeHelpers.CreateBinaryTree(3, tmp1, tmp2);
            var tmp4 = TreeHelpers.CreateBinaryTree(2, tmp3, null);

            root   = TreeHelpers.CreateBinaryTree(0, new BinaryTreeNode <int>(1), tmp4);
            result = new List <List <int> >
            {
                ListHelpers.CreateList(0),
                ListHelpers.CreateList(1, 2),
                ListHelpers.CreateList(3),
                ListHelpers.CreateList(4, 7),
                ListHelpers.CreateList(5, 6, 8, 9)
            };
            ValidateResult(result, root);
        }
Example #17
0
 private static void ValidateResult <T>(Node <T> list, Node <T> nodeToRemove, params T[] expectedResult)
     where T : IEquatable <T>
 {
     Question2_3.RemoveNode(nodeToRemove);
     ListHelpers.ValidateLinkedListContent(list, expectedResult);
 }
Example #18
0
        private static void Validate(bool expectedResult, params int[] values)
        {
            var head = ListHelpers.CreateLinkedList(values);

            Assert.AreEqual(expectedResult, Question2_6.IsPalindrome(head));
        }
Example #19
0
        public void EdgeCaseTest()
        {
            var list = ListHelpers.CreateLinkedList(1, 2, 3);

            Validate(list, null);
        }
        private static Node <Digit> CreateDigitList(int num, bool reversed = false)
        {
            var result = CreateList(num, reversed);

            return(ListHelpers.CreateLinkedList(result.Select(item => new Digit(item)).ToArray()));
        }