## 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

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*/
{
Node *fastNode = head;
{
return 1;
}
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

Haha.

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

I like!

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

haha bc this is called solution..

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

hahah rofl :D

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.

### 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.