Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6980429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:01:01+00:00 2026-05-27T18:01:01+00:00

I was asked in an interview to find if the single linked list has

  • 0

I was asked in an interview to find if the single linked list has a loop. I have searched this SO and found few answers Interview question: How to detect a loop in a linked list? and I knew about those answers before but somehow I answered it different which I had in mind from my discussion with friends long time back, but I wasn’t sure if the answer is correct. If try it myself it works as expected. Somehow the interviewer is not happy. Can someone clarify what is wrong in this ?

struct node {
int flag;
struct node *next;
}

My idea here is not to use two pointers and hop variantly but to use single pointer and traverse each node e.g:

node =  node->next;

Only this I would do is set the flag variable to 1 when ever I traverse the node and check each time e.g

if (node->flag)
{ 
  return TRUE; // implies list is a loop
}
else{
   node->flag = 1;
}

Other than efficiency, what is wrong in this approach ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:01:01+00:00Added an answer on May 27, 2026 at 6:01 pm

    You are using an extra integer variable flag and say if the sizeof(int) in your machine is 4 bytes and say you have 100,000 nodes in the linked-list, then you will end up using 400,000 bytes more. That will be approximately 390M/.38G

    To reset the flag for all the nodes in the linked-list, even though it is O(n), you’ll have to traverse all the 100,000 nodes. Its an overhead.

    Now, if you compare your flag based solution with the 2-variable solution, then it is almost the same computational time but 2-variable solution is considered very memory efficient as it does not use 390M of memory to identify the loop!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question was asked at interview. Say I have a contract. [ServiceContract] public interface
In an interview this is the question that is asked: You have to write
I was asked this question during phone interview. Given two strings find the minimal
This is a interview Question which was asked and wanted to find an efficient
One of my friend was asked this question in an interview - You have
This was the question asked in interview. Can we call one constructor from another
Recently I have been asked an interview question What are the events order in
This is an interview question asked a month ago.... Do session use cookies? If
I was asked this question in a job interview. The interviewer and I disagreed
This was one of the interview questions asked. How to find the length of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.