2
\$\begingroup\$

I was given 15 minutes to write the code to reverse a singly-linked list.

What do you think of the code style here?

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LinkedListQuestions
{
    [TestClass]
    public class ReverseLinkedList
    {
        [TestMethod]
        public void ReservseLinkedListTest()
        {
            Node<int> head = new Node<int>();
            Node<int> two = new Node<int>();
            Node<int> three = new Node<int>();
            Node<int> four = new Node<int>();
            head.Value = 1;
            head.Next = two;
            two.Value = 2;
            two.Next = three;
            three.Value = 3;
            three.Next = four;
            four.Value = 4;
            LinkedListHandler.Reserve(ref head);
            Assert.AreEqual(4, head.Value);
            Assert.AreEqual(3, head.Next.Value);
            Assert.AreEqual(2, head.Next.Next.Value);
            Assert.AreEqual(1, head.Next.Next.Next.Value);
        }
    }

    public class LinkedListHandler
    {
        public static void Reserve(ref Node<int> head)
        {
            Node<int> current = head;
            Node<int> previous = null;
            while (current != null)
            {
                Node<int> temp = current.Next;
                current.Next = previous;
                previous = current;
                current = temp;
            }
            head = previous;
        }
    }

    public class Node<T>
    {
        public T Value { get; set; }
        public Node<T> Next { get; set; }
    }
}
\$\endgroup\$
  • 2
    \$\begingroup\$ What exactly was the interview question (any specific requirements beyond 'reverse a linked list')? And what kind of feedback are you looking for? \$\endgroup\$ – Pieter Witvoet Aug 9 '18 at 7:57
  • 1
    \$\begingroup\$ I think you know that posting a code-only question like this one is off-topic, don't you? ;-) \$\endgroup\$ – t3chb0t Aug 9 '18 at 8:15
  • \$\begingroup\$ @PieterWitvoet and t3chb0t thanks for the comments \$\endgroup\$ – Gilad Aug 9 '18 at 8:25
  • \$\begingroup\$ @t3chb0t Code-only questions are not off-topic by default. \$\endgroup\$ – Snowhawk Aug 9 '18 at 11:51
  • \$\begingroup\$ @Snowhawk well, you should read the linked question. The discussion goes in a slightly different direction there, towards closing and off-topicness. I find it's just plain rude to post code and let people figure out the rest by themselves. \$\endgroup\$ – t3chb0t Aug 9 '18 at 12:00
1
\$\begingroup\$

Test cases right off the bat are a good sign. You only have one, but it is now easier to expand other conditions. I like to add in conditions for list has 0 elements and list is null. You'll be able to prove that those work, or if they don't work, prove that changing it didn't break anything.

Two of the methods use the word Reserve instead of Reverse: ReservseLinkedListTest and Reserve.

For a 15 minute code interview, I'd give it a passing grade.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.