## Samsung Interview Question for Software Engineer Interns

Team: Android
Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
5
of 5 vote

``````public boolean hasCycle(ListNode head) {
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) return true;
}
return false;
}``````

Comment hidden because of low score. Click to expand.
2
of 2 vote

Use two pointers : slow and fast. Slow pointer moves forward by one step and fast by two steps. If your linked list does not have a cycle then your fast will reach the end of linked list and the check will terminate. If there is a cycle, then fast and slow pointer will collide at some point. So, if fast == slow then there is a cycle in the linkedlist.

Comment hidden because of low score. Click to expand.
0
of 0 vote

github.com/techpanja/interviewproblems/blob/master/src/collections/customlinkedlist/LinkedListImpl.java

Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Function returns 1 if it has a cycle and 0 if it doesn't has a cycle*/
int hashCycle(Node *head)
{
Node *fastNode = head;
while(head !=null)
{
if(head->next == fastNode)
return 1;
head=head->next;
}
return 0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

steps:
1)Fist create 2 pointer
slow pointr and fast pointer
2) both point to the starting node
Node *slowPtr=Start;
Node *FastPtr=Start;
3)now forward slow pointer by one step and fast pointer by 2 step
while(Start!=NULL)
{
FastPtr=Fast->next->next
SlowPtr=slow->next
if(SlowPtr==FastPtr)
return 1;
}
4 exit

Comment hidden because of low score. Click to expand.
0
of 0 vote

Node{*nextNode, color='BLACK'}

Node *p = head;
Node *q = p;

while(p -> next != NULL){
q = p->next;
if(q->color == "WHITE"){
printf("Loop found");
break;
}else{
q->color = "WHITE";
}
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

read floyd cycle detection algo.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

``````while (cur != NULL) {
if (cur->data == "cycle") {
return true;
}
cur = cur->next;
}
return false;``````

Comment hidden because of low score. Click to expand.
0
of 0 votes

Haha.

Comment hidden because of low score. Click to expand.
0
of 0 votes

I like!

Comment hidden because of low score. Click to expand.
0
of 0 votes

haha bc this is called solution..

Comment hidden because of low score. Click to expand.
0
of 0 votes

hahah rofl :D

Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

### Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

### Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More