Samsung Interview Question for Software Engineer / Developers


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);

	printf("addess is same %d\n", *slow);
	
	return count;

}

- peter January 14, 2014 | Flag Reply
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

- anonymous January 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- peter January 14, 2014 | Flag
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.

- krylloff January 14, 2014 | Flag Reply
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.

- Murali Mohan January 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

[Adding a missing step]
If the array element is not present, add it to the hashtable.

- Murali Mohan January 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- ulka January 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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?

- dominic June 21, 2014 | Flag
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)

- n.j.joshi January 14, 2014 | Flag Reply
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;
	}

- Serdar January 15, 2014 | Flag Reply
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)
{
dict.Add(i,intar[i]);

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

}
else { dict.Add(i,intar[i]);}
}
}
if (check)
return dict.Count;
return 0;

}

- Vivek January 15, 2014 | Flag Reply
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;
	}

- SAM February 12, 2014 | Flag Reply
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];}
}
}
}

- Anonymous December 17, 2019 | Flag Reply


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