0

Recently I had an interview for a Java software developer and the interviewer asked me some, according to me, stupid questions. One of them was if you have e linked list how to find if there is a cycle in the linked list. Now my question is not how to check for cycle but, I need a real example when this issue will be produced and when would I need to check of the list developing java web applications ?!

Adelin
  • 18,144
  • 26
  • 115
  • 175
  • I used a rotating buffer for a project of mine. That buffer was stored in an array and made to rotate using fancy cursor tracking, but it could just as well have been implemented as a circular buffer. – Adam Oct 30 '14 at 07:46
  • Check out this question, it seems to be similar to what you are asking and it has examples in the answers. http://stackoverflow.com/questions/494830/how-to-determine-if-a-linked-list-has-a-cycle-using-only-two-memory-locations – guribe94 Oct 30 '14 at 07:47
  • What do you mean by `cycle` ? – Rookie007 Oct 30 '14 at 07:48
  • 2
    The questions are rarely meant to be applicable in real life situations, but to test your analytical thinking skills and logic. – Kayaman Oct 30 '14 at 08:04

1 Answers1

0

Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data types, including lists (the abstract data type), stacks, queues, associative arrays, and S-expressions, though it is not uncommon to implement the other data structures directly without using a list as the basis of implementation.{wikipedia}

A malformed linked list with a loop causes infinite iteration over the list to fail because the iteration will never reach the end of the list. Therefore, it is desirable to be able to detect that a linked list is malformed before trying an iteration.So finding loop in linked list will help to avoid bugs and its an basic question from computer science.You should knew it.

vrnithinkumar
  • 1,273
  • 1
  • 11
  • 29
  • This is still books talk, theory . Give me a real example as a developer where you had to check if some linked list has a cycle :) – Adelin Oct 30 '14 at 12:15