Logo F2FInterview

C++ - Data Structures Interview Questions

Q   |   QA

Link list, queue, stack, trees, files, graphs

One possible answer is to add a flag to each element of the list. You could then traverse the list, starting at the head and tagging each element as you encounter it. If you ever encountered an element that was already tagged, you would know that you had already visited it and that there existed a loop in the linked list.

The following algorithm will find the loop:

  1. Start with two pointers ptr1 and ptr2.
  2. Set ptr1 and ptr2 to the head of the linked list.
  3. Traverse the linked list with ptr1 moving twice as fast as ptr2 (for every two elements that ptr1 advances within the list, advance ptr2 by one element).
  4. Stop when ptr1 reaches the end of the list, or when ptr1 = ptr2.
  5. If ptr1 and ptr2 are ever equal, then there must be a loop in the linked list. If the linked list has no loops, ptr1 should reach the end of the linked list ahead of ptr2.

Syntax Error: errors generated by compiler at the time of compilation of program. Logical error: errors in program logic. Runtime error: An error that occurs during the execution of a program, Runtime errors indicate bugs in the program

A tree is a recursive structure that usually maps an ordered set of data from an internal definition to some data space. Tree parts are often named after their contemporaries in family trees; trees contain nodes known as parent, child, and sibling. Trees are made of nodes, which can contain both data to be stored and always link to further levels in the tree. Trees are often formed from a single node known as root; alternatively, trees may be built from a set of original nodes--this is known as a forest of trees

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: