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; }
 }
}
Toby Speight
87.9k14 gold badges104 silver badges325 bronze badges
asked Aug 9, 2018 at 7:34
\$\endgroup\$
7
  • 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\$ Commented Aug 9, 2018 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\$ Commented Aug 9, 2018 at 8:15
  • \$\begingroup\$ @PieterWitvoet and t3chb0t thanks for the comments \$\endgroup\$ Commented Aug 9, 2018 at 8:25
  • \$\begingroup\$ @t3chb0t Code-only questions are not off-topic by default. \$\endgroup\$ Commented Aug 9, 2018 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\$ Commented Aug 9, 2018 at 12:00

1 Answer 1

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.

answered Aug 21, 2018 at 15:42
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.