Interview Question


Country: Canada
Interview Type: In-Person




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

hmmm ok... homework?

for(int i=0; i+2<a.length; i+=3) swap(a[i], a[i+2]);

- S O U N D W A V E October 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

... :)

- Ajeet October 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it will not work where an array is not a multiple of 3
For example:
Input array: {1,9,6,4,5,8,3,1}

- biswajeet156 October 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

oh yeah? does it blow up in black smoke on that case?

- Urik's twin Cookie Monster (dead now) October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Compiled code. Run it as it is.


public class ReverseArray {
	public int[] ReverseArrayInChunk(int[] arr){
		int temp;
		if(arr.length<3){
			return arr;
		}
		for(int i=0;i<arr.length;i=i+3){
			if(i+2<arr.length){
				temp=arr[i];
				arr[i]=arr[i+2];
				arr[i+2]=temp;
				
			}
		}
		
		return arr;	
		}
	public static void main(String[] args){
		int[] a= new int[]{1,2,3,4,5,6,7};
		ReverseArray ra=new ReverseArray();
		int[] b=ra.ReverseArrayInChunk(a);
		for(int j=0;j<b.length;j++){
			System.out.print(b[j]+", ");
		}
	}
}

- Anonymous October 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reverse3(int[] arr)
{
	int i =0;
	int j;
	int temp;
	while(arr.lenght > 0 && i < arr.length-2)
	{
		j= (i+2) > arr.lenght-1 ? i+1 : i+2;
		temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp'
		i =i+3;
	} 
}

- Harsh123 October 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Compiled Java code -

import java.util.Arrays;


public class SwapInThreeChunks {
	public static void main(String[] args)
	{
		int[] input = {1,9,6,4,5,8,3,1,5,2,4,5};
		
		for(int i=0, len=input.length; i<len; i++)
		{
			if(i+2 < len)
			{
				int temp = input[i];
				input[i] = input[i+2];
				input[i+2] = temp;
			}
			i += 2;
		}
		
		System.out.println(Arrays.toString(input));
	}
}

- ruharish October 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution is clean. However, don't you need to swap the last two elements if input.length % 3 is 2?

- Kelvin November 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
* Reverse number in each chunk of k items in an array
*/
private static void reverseChunk(int[] raw, int k) {
if (k <= 1) return;
for(int i=0; i<raw.length; i=i+k) {
reverseChunk(raw, i, k);
}
}

private static void reverseChunk(int[] raw, int start, int k) {
if (start >= raw.length - 1) {
return;
}
int end = Math.min(raw.length - 1, start + k - 1);
if (end == start) {
return;
}
int j = start;
while (j < end + start - j) {
int tmp = raw[j];
raw[j] = raw[end + start -j];
raw[end + start - j] = tmp;
j++;
}
}

- Anonymous October 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
* Reverse number in each chunk of k items in an array
*/
private static void reverseChunk(int[] raw, int k) {
if (k <= 1) return;
for(int i=0; i<raw.length; i=i+k) {
reverseChunk(raw, i, k);
}
}

private static void reverseChunk(int[] raw, int start, int k) {
if (start >= raw.length - 1) {
return;
}
int end = Math.min(raw.length - 1, start + k - 1);
if (end == start) {
return;
}
int j = start;
while (j < end + start - j) {
int tmp = raw[j];
raw[j] = raw[end + start -j];
raw[end + start - j] = tmp;
j++;
}
}

- Anonymous October 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int [] swapEle(int []A, int k){
		int i=0, l=A.length, j;
		while(i<l){
			
			j=i+k-1;
			if(j>=l){
				j=l-1;
			}
			while(i<j){
				swap(A, i, j);
				i++;
				j--;
			}
			if(k%2==0){
				i+=(k/2);
			} else i+=(k/2+1);
		}
		return A;
	}

- shashi_kr October 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void swap(int &a, int &b)
{
	int tmp=a;
	a=b;
	b=tmp;
}

void SwapInThrees(int *a, int n)
{
	int i;
	if(a==NULL || n<3)
		return;
	for(i=1;i<=n-2;)
	{
		swap(a[i+1],a[i-1]);
		i+=3;
	}

}

- Anonymous October 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is working c++ code:

void reverse_chunks(int a[],int size, int chunk)
{
int index=0;
for(int i=0;i<size/chunk;index+=chunk,++i)
reverse_chunk(a,index,index+chunk-1);
if(index<size)
reverse_chunk(a,index,size-1);
}

- Dinesh November 18, 2013 | 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