## Samsung Interview Question for Software Engineer / Developers

• 0

Country: United States

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

``````int cycle(int a[], int num){
int count = 0;

//find a cycle
int *fast = a;
int *slow = a;
int *start = a;

do{
fast = &a[*fast];
fast = &a[*fast];

slow = &a[*slow];
}while(fast != slow);

//fix slow pointer, and move another pointer to run a cycle
start = slow;
do
{
start = &a[*start];
//slow = &a[*slow];
count++;
}while(start != slow);

return count;

}``````

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

shouldn't length b 4 since cycle consists of 2,3,1,0

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

right, the answer should be 4, not 3. I changed an example, but not the answer. sorry for making you confusing.

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

It is possible to create oriented graph where the edge (x, y) exists if a[x] = y. After that use Deep First Search to find loop.

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

Use a hashtable that stores value of the array as 'key' and index as 'value'. Iterate through the array, each time checking if the array element is present in the hashtable. If yes, it implies that there is a cycle in the array and the length of the cycle = current_array_index - 'value' in the hashtable.

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

If the array element is not present, add it to the hashtable.

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

However here if array contains wide range of numbers lets say cycle is like
2->200->3000->30000 then what is hashtable size?

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

let cycle start at value x, such that a-> b-> x->y->z->x,
the loop would stop at putting x into the hashtable
let traverse the list again from the start and count until x
so the size of cycle would be size of hashtable - count?

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

One way to do is search for strongly-connected components:

``````tSortedList = TopoSort(a); // O(V + E), here |E| = |V|, so O(V)
aT = transpose(a);   // O(V)
run DFS(aT);    // O(V)
on the strongly connected components
return max (length(component) );    // O(V)``````

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

``````static int detectCycle(int[] a) {

for(int i =1; i< a.length; i++) {
if(a[i] == a[0]){

//Cycle possible
boolean cycleDetected = true;
for(int j = 1; j< i && j+i<a.length; j++) {
if(a[j] != a[j+i]) {

//Not a cycle
cycleDetected = false;
break;
}
}//for

if(cycleDetected) {
//Cycle detected
return i;
}
}
}

//No cycle
return -1;
}``````

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

public int CycleLength(int[] intar )
{
bool check = true;
Dictionary<int, int> dict = new Dictionary<int, int>();
for (int i = 0; i < intar.Length; i++)
{
if (dict.Count == 0)
{

}
else
{
if (dict.ContainsValue(intar[i]))
{
if (!(dict[(i % dict.Count)] == intar[i]))
{
check = false;
}

}
}
}
if (check)
return dict.Count;
return 0;

}

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

``````public static boolean detectcycle(int array[]){
int startindex= 0;
int count=1;
for(int i=1;i<=array.length/2;i++){
//detect cycle
if(array[i]==array[startindex]){
//store lastIndex or one cycle
int lastIndex=i-1;
//System.out.println(lastIndex);
while(startindex<=lastIndex&&i<array.length){
if(array[i]==array[startindex]){
i++;
startindex++;
}
else {
System.out.println("no cycle found");
return false;
}

}

System.out.println("cycle length is : "+count);

}
else{
count++;
}
}
return true;
}``````

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

``````var B = [];
for( i = 0 ;i< A.length ;i++){
c= 0;
B[c] = A [i] ;
for(j=i+1;j<A.length;j++){
if(A[i]!=A[j])B[++c] = A[j] ;

else{
k= 0;p =j;
while(k<=c){
if(A[p]!=B[k])
break;
p++;k++;
}
if(k==c+1){
console.log("Pattern found" + j + c ) ;
for(t =0;t<=c;t++)
console.log( B[t] );
i= A.length;j = A.length
}
else{
console.log(c,j);
B[++c] = A[j];}
}
}
}``````

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.