Amazon Interview Question for Quality Assurance Engineers


Team: Kindle
Country: India
Interview Type: Written Test




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

Java solution, complexity O(n * k):

private int[] reverseArray(int[] A, int k) {
        for (int i = 0; i < A.length; i += k) {
            int left = i;
            // in case right larger than A.length
            int right = Math.min(i + k - 1, A.length - 1);

            // reverse sub array
            while (left < right) {
                int temp = A[left];
                A[left] = A[right];
                A[right] = temp;

                left++;
                right--;
            }
        }

        return A;
    }

- techinterviewquestion.com May 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def revRange(array):

revArray = []

for k in range(1, len(array)+1):

k = k*(-1)

revArray.append(array[k])

return (revArray)



def revSubSet(l, N):

x = 0
rev_Sub_Set = []

for i in range(1,(len(l)//N)+1):

subSet = []

for j in range(x,N*i):

subSet.append(l[j])

x = N*i

y = (subSet)

rev_Sub_Set = rev_Sub_Set + y

subSet = []

leftOuts = []

for p in range(0, len(l)):

if l[p] not in rev_Sub_Set:

leftOuts.append(l[p])

if len(leftOuts) > 0:

y = revRange(leftOuts)

rev_Sub_Set = rev_Sub_Set + y

return (rev_Sub_Set)


l = [1,2,3,4,5,6,7,8,9]
N = 3

print (revSubSet(l,N))

- Prashant May 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def revRange(array):

revArray = []

for k in range(1, len(array)+1):

k = k*(-1)

revArray.append(array[k])

return (revArray)



def revSubSet(l, N):

x = 0
rev_Sub_Set = []

for i in range(1,(len(l)//N)+1):

subSet = []

for j in range(x,N*i):

subSet.append(l[j])

x = N*i

y = (subSet)

rev_Sub_Set = rev_Sub_Set + y

subSet = []

leftOuts = []

for p in range(0, len(l)):

if l[p] not in rev_Sub_Set:

leftOuts.append(l[p])

if len(leftOuts) > 0:

y = revRange(leftOuts)

rev_Sub_Set = rev_Sub_Set + y

return (rev_Sub_Set)


l = [1,2,3,4,5,6,7,8,9]
N = 3

print (revSubSet(l,N))

- Prashant May 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def revertSubsetList(l,n,res=[]):
    for i in range(1, len(l)/n+1):
        for j in range(1,n+1):
            res += [ l[i*n-j] ]
    return res

print revertSubsetList([1,2,3,4,5,6,7,8,9,10,11,12], 3)

- Alain May 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you need to add int() to len(l)/n+1 as the float values won't be supported

- Ridhibhan.19 December 20, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> input { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	
	const int REVERSE_SIZE = 3;
	
	int front = 0;
	int swaps = 0;
	
	while( front < input.size() )
	{
	    int back = front + ((REVERSE_SIZE - (swaps * 2) - 1));
		if( back >= input.size() )
	    {
	        break;
		}
	    std::swap( input[front], input[back] );
		++swaps;
		++front;
		if( swaps == (REVERSE_SIZE / 2.0) )
		{
		    front += REVERSE_SIZE / 2.0;
			swaps = 0;
		}
		else if ( swaps == (REVERSE_SIZE / 2 ) ) // implicit truncation
		{
		    front += (REVERSE_SIZE / 2.0) + 0.5;
			swaps = 0;
		}
	}
	for( const auto& num : input )
	{
	    std::cout << "\n" << num;
	}
}

- merlinme May 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In python:

def revert_by_n(in_lst, n):
    result = []
    ndx = 0
    lst_ndx = min(len(in_lst), ndx + n)
    while ndx < len(in_lst):
        for i in xrange(lst_ndx, ndx, -1):
            result.append(in_lst[i - 1])
        if lst_ndx >= len(in_lst) + 1:
            break
        ndx += n
        lst_ndx = min(len(in_lst), lst_ndx + n)
    return result

Test:

tst = [
    ([1,2,3,4,5,6,7,8,9], 3, [3,2,1,6,5,4,9,8,7]),
    ([1,2,3,4,5,6,7,8], 3, [3,2,1,6,5,4,8,7]),
    ([1, 2, 3], 2, [2, 1, 3]),
    ([1, 2, 3], 4, [3, 2, 1]),
    ([1, 2, 3], 1, [1, 2, 3]),
    ]

for in_lst, n, exp in tst:
    res = revert_by_n(in_lst, n)
    assert res == exp, 'in: {}, n: {} -> {} != {}'.format(in_lst, n, res, exp)
    print '{}: {} -> {}'.format(in_lst, n, res)

- gustavo.andriotti May 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void rev(int[] input,int n)
  {
      if(input==null || input.length<2 || n<2 ||input.length<n)
          return ;
        int i=0;
        int curEnd = n;
        int j = curEnd-1;
       while(i>=0&& j<input.length&& i<input.length)
       {
      
           int temp =  input[i];
           input[i] = input[j];
           input[j] = temp;
           
           i++;
           j--;
           
           if(i>=j-1)
               {
               i = curEnd;
               curEnd = i+n;
               j = curEnd-1;
           }
       }

}

- Anonymous May 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void rev(int[] input,int n)
  {
      if(input==null || input.length<2 || n<2 ||input.length<n)
          return ;
        int i=0;
        int curEnd = n;
        int j = curEnd-1;
       while(i>=0&& j<input.length&& i<input.length)
       {
      
           int temp =  input[i];
           input[i] = input[j];
           input[j] = temp;
           
           i++;
           j--;
           
           if(i>=j-1)
               {
               i = curEnd;
               curEnd = i+n;
               j = curEnd-1;
           }
       }

}

- skumaringuva May 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Solution {
	public static void main(String[] args) {
		Integer[] a = {1,2,3,4,5,6,7,8,9};		
		printArray(getReversedNSectionArray(a, 3));
	}
	
	private static void printArray(Integer[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}		
	}

	public static Integer[] getReversedNSectionArray(Integer[] a, int n) {
		if (a == null || n > a.length || n < 1) {
			return null;
		}		
		if (n == 1) {
			return a;
		}
		int len = a.length;
		
		// if n = len just return reversed array
		if (n == len) {			
			return (Integer[]) Arrays.asList(a).stream().sorted((f1, f2) -> Integer.compare(f2, f1) * -1).toArray();
		}
		
		int s = -1; // sector size
		int rem = len; // remaining elements
		int i = 0;
		int j = 0;
		int k = 0;
		
		while (i < len) {
			s = (n > rem) ? rem : n; // possible sector size
			j = i;
			k = i + s - 1;
			
			while (j < k) {
				swap(a, j, k);
				j++;
				k--;
			}
			i += s;
			rem -= s;
			
		}
		return a;
	}
	
	private static void swap(Integer[] arr, int i, int j) {
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}	
}

- guilhebl May 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Similar like previous posts

void ReverseBySubset(int[] a, int n)
{
    for (int i=0; i < a.Length; i+=n)
    {
        int start = i;
        int end = i + (n-1);
        if (end >= a.Length)
            break;

        while (start < end)
        {
            int tmp = a[start];
            a[start] = a[end];
            a[end] = tmp;

            start++;
            end--;
        }
    }
}

- hnatsu May 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Objects;

public class ArrayUtil {
	public static void reverseArray(int arr[], int k) {
		if (Objects.isNull(arr) || arr.length == 1 || k <= 1 || k > arr.length)
			return;

		int length = arr.length;
		int iter = length / k;
		int start, stop;

		for (int i = 0; i < iter; i++) {
			start = i * k;
			stop = ((i + 1) * k) - 1;
			reverse(arr, start, stop);
		}

		int rem = length % k;
		start = (length - rem);
		stop = length - 1;

		reverse(arr, start, stop);

	}

	private static void reverse(int arr[], int start, int stop) {
		while (start < stop) {
			swap(arr, start, stop);
			start++;
			stop--;
		}
	}

	private static void swap(int arr[], int start, int stop) {
		int temp = arr[start];
		arr[start] = arr[stop];
		arr[stop] = temp;
	}

}

- harikrishna553 May 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void reverseSubArr(int[] a, int k) {
		int startPtr = 0;
		int endPtr = k-1;
		int temp;
		while(startPtr < endPtr) {
			temp = a[startPtr];
			a[startPtr] = a[endPtr];
			a[endPtr] = temp;
			startPtr++;
			endPtr--;
			
		}
	}

- Alok Mishra May 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class reverseSubarray {
	public static void main(String[] args) {
		int arr[]= {3, 2, 1, 6, 5, 4, 9, 8, 7,11, 10};
		
		for (int i =0; i<arr.length;i+=2){
			for (int j = i+1;j>i;j--){
				if(j>arr.length-1)
					break;
				int temp =arr[i];
				arr[i]=arr[j];
				arr[j]=temp;
				System.out.println(arr[i] + " - " + arr[j]);
				//System.out.println(arr[j]);
			}
		}
		System.out.println(arr[arr.length-1]);
	}
}

- alok May 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrayReverseProblem {
public static void main(String[] args) {
int inputArray[] = {1,2,3,4,5,6,7,8,9};
int revreseIndex=3;
int NextrevreseIndex=revreseIndex+3;
int FinalrevreseIndex=NextrevreseIndex+3;
String first = "",second="",third="";

for(int i=1;i<=inputArray.length;){
if(i<=revreseIndex){
first+=inputArray[revreseIndex-i]+",";
}
if((i<=revreseIndex)){
second+=inputArray[NextrevreseIndex-i]+",";
}
if((i<=revreseIndex)){
third+=inputArray[FinalrevreseIndex-i]+",";
}
++i;
}
System.out.println("["+first+second+third.substring(0, third.length()-1)+"]");

}

}

- sumit kaushik May 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <array>
#include <iostream>

int main()
{
    std::array<int, 9> input { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	
	const int REVERSE_SIZE = 3;
	const int ORIGINAL_FRONT = 0;
	int front = ORIGINAL_FRONT;
	int back = ORIGINAL_FRONT + (REVERSE_SIZE - 1);
	
	while( back < input.size() )
	{
		int next_front = front + REVERSE_SIZE;
		while( front < back )
		{
		    std::swap( input[front++], input[back--] );	
		}
		front = next_front;
		back = front + (REVERSE_SIZE - 1);
	}
	for( const auto& num : input )
	{
	    std::cout << num << "\n" ;
	}
}

- merlinme May 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseSubset {

	public static void main(String[] args) {
		int arr[] = {1,2,3,4,5,6,7,8};
		int N =5;
		
		int l = arr.length;
		int end = (N-1)/2;
		
		int i,temp,k=0;
		for(i=0;i<end;i++)
		{
			temp= arr[i];
			arr[i]=arr[N-k-1];
			arr[N -k-1]=temp;
			k++;
		}
		
		for(i=0;i<l;i++)
			System.out.println(arr[i]);
		
	}
}

- Rehman May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseSubset {

	public static void main(String[] args) {
		int arr[] = {1,2,3,4,5,6,7,8};
		int N =5;
		
		int l = arr.length;
		int end = (N-1)/2;
		
		int i,temp,k=0;
		for(i=0;i<end;i++)
		{
			temp= arr[i];
			arr[i]=arr[N-k-1];
			arr[N -k-1]=temp;
			k++;
		}
		
		for(i=0;i<l;i++)
			System.out.println(arr[i]);
		
	}
}

- Rehman May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import math

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n = 3

for i in range(0, int(math.ceil(len(arr)/n))):
    for j in range(0, n/2):
        arr[(n*i) + j], arr[(n*i) + n - j - 1] = arr[(n*i) + n - j - 1], arr[(n*i) + j ]

print arr

- Elhafyani May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <vector>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char const *argv[]) {
int v[] ={1,2,3,4,5,6,7,8,9};
int temp = 0,size = 3;
for (int i = 0; i < 9; i = i + 3) {
temp = v[i];
v[i] = v[size - i - 1];
v[size - i - 1] = temp;
size = size + 6;
}
for (int i = 0; i < 9; i++) {
printf("%d\n", v[i]);
}
return 0;
}

- kiruba ebenezer May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <vector>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char const *argv[]) {
  int v[] ={1,2,3,4,5,6,7,8,9};
  int temp = 0,size = 3;
  for (int i = 0; i < 9;  i = i + 3) {
      temp = v[i];
      v[i] = v[size - i - 1];
      v[size - i - 1] = temp;
      size = size +  6;
  }
  for (int i = 0; i < 9; i++) {
    printf("%d\n", v[i]);
  }
  return 0;
}

- kiruba ebenezer May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <vector>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char const *argv[]) {
  int v[] ={1,2,3,4,5,6,7,8,9};
  int temp = 0,size = 3;
  for (int i = 0; i < 9;  i = i + 3) {
      temp = v[i];
      v[i] = v[size - i - 1];
      v[size - i - 1] = temp;
      size = size +  6;
  }
  for (int i = 0; i < 9; i++) {
    printf("%d\n", v[i]);
  }
  return 0;
}

- kiruba ebenezer May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <vector>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char const *argv[]) {
  int v[] ={1,2,3,4,5,6,7,8,9};
  int temp = 0,size = 3;
  for (int i = 0; i < 9;  i = i + 3) {
      temp = v[i];
      v[i] = v[size - i - 1];
      v[size - i - 1] = temp;
      size = size +  6;
  }
  for (int i = 0; i < 9; i++) {
    printf("%d\n", v[i]);
  }
  return 0;
}

- kiruba ebenezer May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <vector>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char const *argv[]) {
int v[] ={1,2,3,4,5,6,7,8,9};
int temp = 0,size = 3;
for (int i = 0; i < 9; i = i + 3) {
temp = v[i];
v[i] = v[size - i - 1];
v[size - i - 1] = temp;
size = size + 6;
}
for (int i = 0; i < 9; i++) {
printf("%d\n", v[i]);
}
return 0;
}

- kirubstkp May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This code will give performance of O(n/2).

public static void reverseSubset(int[] a, int k){
        System.out.println("reversing array "+a+" with "+k);
        if(a.length > k){
            int lo = 0;
            int hi = k-1;
            while(lo<hi) {
                reverse(a, lo, hi);
                lo = hi+1;
                hi = lo+k-1;
                hi = Math.min(hi,a.length-1);
            }
        }
    }

    private static void reverse(int[] a, int lo, int hi) {
        System.out.println("reverse(a, "+lo+", "+hi+")");

        while(lo<hi){
            swap(a,lo++,hi--);

        }

    }

    private static void swap(int[] a, int i, int j) {
        System.out.println("swap(a, "+i+", "+j+")");
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    public static void main(String[] args) {
               int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13};
        reverseSubset(a,2);
        reverseSubset(a,3);
        reverseSubset(a,4);
        reverseSubset(a,5);
    }

Here are the sysout on running main
reversing array [I@511d50c0 with 2
reverse(a, 0, 1)
swap(a, 0, 1)
reverse(a, 2, 3)
swap(a, 2, 3)
reverse(a, 4, 5)
swap(a, 4, 5)
reverse(a, 6, 7)
swap(a, 6, 7)
reverse(a, 8, 9)
swap(a, 8, 9)
reverse(a, 10, 11)
swap(a, 10, 11)
reversing array [I@511d50c0 with 3
reverse(a, 0, 2)
swap(a, 0, 2)
reverse(a, 3, 5)
swap(a, 3, 5)
reverse(a, 6, 8)
swap(a, 6, 8)
reverse(a, 9, 11)
swap(a, 9, 11)
reversing array [I@511d50c0 with 4
reverse(a, 0, 3)
swap(a, 0, 3)
swap(a, 1, 2)
reverse(a, 4, 7)
swap(a, 4, 7)
swap(a, 5, 6)
reverse(a, 8, 11)
swap(a, 8, 11)
swap(a, 9, 10)
reversing array [I@511d50c0 with 5
reverse(a, 0, 4)
swap(a, 0, 4)
swap(a, 1, 3)
reverse(a, 5, 9)
swap(a, 5, 9)
swap(a, 6, 8)
reverse(a, 10, 12)
swap(a, 10, 12)

- Priyanshu Jain May 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This will give performance of O(n/2)

/**
 * Created by pJain on 5/27/16.
 */
public class ReverseArraySubSet {

    public static void reverseSubset(int[] a, int k){
        System.out.println("reversing array "+a+" with "+k);
        if(a.length > k){
            int lo = 0;
            int hi = k-1;
            while(lo<hi) {
                reverse(a, lo, hi);
                lo = hi+1;
                hi = lo+k-1;
                hi = Math.min(hi,a.length-1);
            }
        }
    }

    private static void reverse(int[] a, int lo, int hi) {
        System.out.println("reverse(a, "+lo+", "+hi+")");

        while(lo<hi){
            swap(a,lo++,hi--);

        }

    }

    private static void swap(int[] a, int i, int j) {
        System.out.println("swap(a, "+i+", "+j+")");
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    public static void main(String[] args) {
        int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13};
        reverseSubset(a,2);
        reverseSubset(a,3);
        reverseSubset(a,4);
        reverseSubset(a,5);

    }
}

- pJain May 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class ReverseArray {
	public static void arrayWithSubsetN(int[] array, int n) {
		int i = 0;
		while (i < array.length) {
			int start = i;
			int j = i;
			int end = (i + n) - 1;
			boolean flag = false;
			for (j = i ; j < end && end < array.length; j++) {
				if (array[j] > array[j + 1]) {
					flag = false;
					break;
				}
				flag = true;
			}
			if (flag) {
				while (start < end) {
					int tmp = array[start];
					array[start] = array[end];
					array[end] = tmp;
					start++;
					end--;
				}
				i = ((i + n) - 1) + 1;
				continue;
			}
			i++;
		}
	}
}
public class ArrayReverse {
	public static void main(String[] args) {
		int[] array = {1,2,3,4,5,6,7,8,9,10,12};
		ReverseArray.arrayWithSubsetN(array, 3);
		for (int i = 0; i< array.length; i++) {
			System.out.print(array[i] + " ");
		}
	}
}

- anand May 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

int main() {
int arr[] = {1, 2, 3, 6, 7, 8, 9, 5, 4, 12, 15, 18, 17, 25, 20};
int s = sizeof(arr)/sizeof(arr[0]);
int k=0;
cout << " Enter k : ";
cin >> k;
for(int i=0; i<(s/k); i++) {
int t=0;
for(int j=(i*k); j< (i*k) + k/2; j++) {
int l = arr[j+t];
arr[j+t] = arr[j+k-1-t];
arr[j+k-1-t] = l;
t++;
}
}
return 0;
}

- kbkunalb May 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseInSubsets {

	public static void main(String args[])
	{
		int arr[]={1,2,3,4,5,6,7,8,9};
		int n=4;
		int temp=0;
		for(int i=0;i<arr.length;i=i+n)
		{
			int j=i;
			int k=n+i-1;
			while(j<=k&&j<arr.length)
			{	
				
				if(k>=arr.length)
				{
					
					k--;
				}
				else{	
					temp=arr[j];
					arr[j]=arr[k];
					arr[k]=temp;
					j++;
					k--;
				}
			}
			
		}
		for(int i=0;i<arr.length;i++)
		{
			System.out.println(arr[i]);
		}
		
		
	}

}

- Anonymous June 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseInSubsets {

	public static void main(String args[])
	{
		int arr[]={1,2,3,4,5,6,7,8,9};
		int n=4;
		int temp=0;
		for(int i=0;i<arr.length;i=i+n)
		{
			int j=i;
			int k=n+i-1;
			while(j<=k&&j<arr.length)
			{	
				
				if(k>=arr.length)
				{
					
					k--;
				}
				else{	
					temp=arr[j];
					arr[j]=arr[k];
					arr[k]=temp;
					j++;
					k--;
				}
			}
			
		}
		for(int i=0;i<arr.length;i++)
		{
			System.out.println(arr[i]);
		}
		
		
	}
	
	
}

- Abishek Chiff June 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseInSubsets {

	public static void main(String args[])
	{
		int arr[]={1,2,3,4,5,6,7,8,9};
		int n=4;
		int temp=0;
		for(int i=0;i<arr.length;i=i+n)
		{
			int j=i;
			int k=n+i-1;
			while(j<=k&&j<arr.length)
			{	
				
				if(k>=arr.length)
				{
					
					k--;
				}
				else{	
					temp=arr[j];
					arr[j]=arr[k];
					arr[k]=temp;
					j++;
					k--;
				}
			}
			
		}
		for(int i=0;i<arr.length;i++)
		{
			System.out.println(arr[i]);
		}
		
		
	}
	
	
}

- AbishekM June 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def subset_reverse(let_list, N):
	result = []
	for i in range(0, len(let_list), N):
		result += let_list[i:i+N][::-1]
	return result

- Anonymous June 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

are we allowed to code in our own IDE and paste in HackerRank or they want you to directly write the code in the HackerRank notepad format?

- sara June 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseSet {

public static void main(String[] args) {
ArrayList<Integer> a= new ArrayList();
int aa [] = {1,2,3,4,5,6,7,8,9};
int n ;
Scanner in = new Scanner(System.in);
System.out.println("Input n");
n = in.nextInt();
int r =0;
int p=n;
loopu:
while(true){
int b[] =Arrays.copyOfRange(aa, r, n);
for (int i=p-1;i>=0;i--){
System.out.print(b[i]);;
a.add(b[i]);
}
r=n;
n= n+p;
if (n>aa.length)
{break loopu;}

}
System.out.println(a);
}

}

- Anonymous June 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseSet {

public static void main(String[] args) {
ArrayList<Integer> a= new ArrayList();
int aa [] = {1,2,3,4,5,6,7,8,9};
int n ;
Scanner in = new Scanner(System.in);
System.out.println("Input n");
n = in.nextInt();
int r =0;
int p=n;
loopu:
while(true){
int b[] =Arrays.copyOfRange(aa, r, n);
for (int i=p-1;i>=0;i--){
System.out.print(b[i]);;
a.add(b[i]);
}
r=n;
n= n+p;
if (n>aa.length)
{break loopu;}

}
System.out.println(a);
}

}

- Anonymous June 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python code for solving this problem , The idea is to slice the list with N and create subarrays of length N , reverse it and then return the array

import itertools

def reversesubset(l,n):
    x =[]
    for i in xrange(0,len(l),n):
        y =l[i:(i+n)]
        y.reverse()
        x.append(y)
    print list(itertools.chain(*x))

reversesubset([1,2,3,4,5,6,7,8,9,10,11,12], 3)

- sri June 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package test;

import java.util.Arrays;

public class AmazonReverseSubSet {

	
	
	public static void main(String[] args) {
		int[] x = { 9, 2, 4, 7, 3, 7, 12, 15, 10 };
		int n = 4;

		process (x, n);
		
	}
	
	public static void process (int[] array, int n){
		
		int indexRight=n-1;
		
		int totalIterations= (n/2);
		
		for (int left=0,rigth=totalIterations; rigth>0 && left<array.length;left++,rigth-- ){
			
			int aux= array[indexRight];
			array[indexRight] =  array[left];
			array[left] = aux;	
			indexRight--;
			
		}
		
		
		System.out.println(Arrays.toString(array));
	}

}

- israAzul June 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void swap(int arr[],int i,int j)
{
	int temp = arr[i];
	arr[i] = arr[j];
	arr[j] = temp;
}


int _tmain(int argc, _TCHAR* argv[])
{

	int arr[6] = {2,4,6,8,3,67};
	int size = 6;
	
	int swapcount =2;

	for(int j=0;j<size;j=j+swapcount)
	{
		int i =j;
		int maxswap = i+ swapcount-1;
		while(maxswap>size-1)
		{
			maxswap--;
		}
		
		while(i<maxswap)
		{
			swap(arr,i++,maxswap--);
		
		}
	}



	return 0;
}

- Anonymous June 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void swap(int arr[],int i,int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}


int _tmain(int argc, _TCHAR* argv[])
{

int arr[6] = {2,4,6,8,3,67};
int size = 6;

int swapcount =2;

for(int j=0;j<size;j=j+swapcount)
{
int i =j;
int maxswap = i+ swapcount-1;
while(maxswap>size-1)
{
maxswap--;
}

while(i<maxswap)
{
swap(arr,i++,maxswap--);

}
}



return 0;
}

- Anonymous June 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void swap(int arr[],int i,int j)
{
	int temp = arr[i];
	arr[i] = arr[j];
	arr[j] = temp;
}


int _tmain(int argc, _TCHAR* argv[])
{

	int arr[6] = {2,4,6,8,3,67};
	int size = 6;
	
	int swapcount =2;

	for(int j=0;j<size;j=j+swapcount)
	{
		int i =j;
		int maxswap = i+ swapcount-1;
		while(maxswap>size-1)
		{
			maxswap--;
		}
		
		while(i<maxswap)
		{
			swap(arr,i++,maxswap--);
		
		}
	}



	return 0;
}

- Ujwalpcs June 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseArraySubSet {

private static Scanner in;

public static void main(String args[]) {

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

System.out.println("The array is:");
for (int x = 0; x < array.length; x++) {
System.out.print(" "+array[x]);
}
System.out.println();
System.out.println("Enter the size of subset to be reversed. it should be be less than array size");
in = new Scanner(System.in);
int n = in.nextInt();
int i = 0;
int j = n - 1;

while (i < n / 2) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}

System.out.println("Print the array");
for (int x = 0; x < array.length; x++) {
System.out.print(" "+array[x]);
}
}
}

- kasidkhan July 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseArraySubSet {

	private static Scanner in;

	public static void main(String args[]) {

		int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

		System.out.println("The array is:");
		for (int x = 0; x < array.length; x++) {
			System.out.print(" "+array[x]);
		}
		System.out.println();
		System.out.println("Enter the size of subset to be reversed. it should be be less than array size");
		in = new Scanner(System.in);
		int n = in.nextInt();
		int i = 0;
		int j = n - 1;

		while (i < n / 2) {
			int temp = array[i];
			array[i] = array[j];
			array[j] = temp;
			i++;
			j--;
		}

		System.out.println("Print the array");
		for (int x = 0; x < array.length; x++) {
			System.out.print(" "+array[x]);
		}
	}
}

- kasidkhan July 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = [1,2,3,4,5,6,7,8,9,10,11]
b = Array.new
l = a.length
n = 5
start=0
endv=n
(0...l).step(n) do |i|
	for j in (endv-1).downto(start)
		b << a[j]
	end
	start= start + n
	endv = endv + n
end
puts b

- sonyjames9 July 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def subStringReverse(my_arr, n):
    if n <= len(my_arr):
        sub_arr1 = my_arr[n-1::-1]
        sub_arr2 = my_arr[n:]
        return sub_arr1+sub_arr2
    return 0
my_arr = [1,2,3,4,5,6,7,8,9]
print subStringReverse(my_arr, 6)

- CuriousGeorge July 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseArraySubset {
	public static void main(String[] args) {
		int[] inp = {1,2,3,4,5,6,7,8,9};
		int length = inp.length;
		int n = 3;
		for(int a : inp)
			System.out.print(a + "\t");
		System.out.println();
		inp = reverse(inp,n);
		for(int a : inp)
			System.out.print(a + "\t");
	}
	
	static int[] reverse(int[] arr, int n) {
		int i=0;
		while(i<=arr.length-1) {
			int j = i+n-1;
			int k=j;
			while(i<j && j<=arr.length-1) {
				swap(arr, i , j);
				i++;
				j--;
			}
			i = k + 1;
		}
		return arr;
	}
	
	static void swap(int[] a, int i, int j) {
		int tmp = a[i];
		a[i] = a[j];
		a[j] = tmp;
	}

}

- nsj59 August 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Resverse_arr_subsetofN {

public static void main(String[] args) {

int[] myIntArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int subset = 3;
int i = 0;

while (i <= myIntArray.length - 1) {
int j = 0;
ArrayList<Integer> mySubArray = new ArrayList<Integer>();
while (j < subset) {

mySubArray.add(myIntArray[i]);

j++;
i++;

}
for ( int k = mySubArray.size()-1 ; k >= 0 ; k-- )
{
System.out.print(mySubArray.get(k)+" ");
}
System.out.println();

}

}

}

- chaitu95test August 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Time -O(n) ...Actually less than that

public class ArraYRevKth {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int k = 5;
		int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };
		int k1 = k;
		int index = 0;

		for (int i = 0; i < arr.length; i++) {
			if (i + k >= arr.length) {
				k = arr.length - i - 1;
			}
			int temp = arr[i + k];
			arr[i + k] = arr[i];
			arr[i] = temp;
			k = k - 2;
			index++;
			if (k <= 0) {
				i = i + index;
				k = k1;
				index = 0;
			}

		}
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
	}

}

- Prosun August 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.*;

class myCode
{
public static void main (String[] args) throws java.lang.Exception
{
int[] arr = {1,2,3,4,5,6,7,8,9};
int k =3;
int[] res = rev(arr,k);
for(int i =0; i<res.length;i++){
System.out.println(res[i]);
}
}

public static int[] rev(int[] nums, int n){

for(int i=0; i<nums.length; i=i+n){

int j = i+n-1;
// System.out.println(nums[i] + " and j value is " + nums[j]);
int k =i;
while(k<=j){
int temp = nums[k];
nums[k] = nums[j];
nums[j] = temp;
j--;
k++;
}

}
return nums;
}

}

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

import java.io.*;

class myCode
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int[] arr = {1,2,3,4,5,6,7,8,9};
        int k =3;
        int[] res = rev(arr,k);
        for(int i =0; i<res.length;i++){
       System.out.println(res[i]);
    }
    }

    public static int[] rev(int[] nums, int n){
        
        for(int i=0; i<nums.length; i=i+n){
            
            int j = i+n-1;
           // System.out.println(nums[i] + " and j value is " + nums[j]);
            int k =i;
            while(k<=j){
            int temp = nums[k];
                nums[k] = nums[j];
                nums[j] = temp;
            j--;
                k++;
            }
            
        }
   return nums; 
    }
    
}

- gravity January 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def fun(arr,sub):
a = 0
l = len(arr)
li = list()
print(l)
n =int(l/sub)
print(n)
for p in range(n):
z = arr[a:a+3]
z.sort()
for i in z:

li.append(i)
#li.sort()
a += 3

return li

ar = [1,3,5,7,9,11,17,19,15]
s = 3
print(fun(ar,s))

- Aasahy June 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def fun(arr,sub):
    a = 0
    l = len(arr)
    li = list()
    print(l)
    n =int(l/sub)
    print(n)
    for p in range(n):
        z = arr[a:a+3]
        z.sort()
        for i in z:

            li.append(i)
        #li.sort()
        a += 3

    return li

ar = [1,3,5,7,9,11,17,19,15]
s = 3
print(fun(ar,s))

- Aasahy June 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void reverseArrN(int[] a, int n){
		int len = a.length; //9-3=6
		//int k = n-1;
		if(a.length==0){
			System.out.println("empty array");
		}
		else if(a.length==1){
			System.out.println("Array has only 1 element");
		}
		else if(a.length<n){
			System.out.println("reverse entire array or invalid usecase a n cannot be greater than length of array.");
		}
		else{
			for(int i=0;i<len;i+=n-1){
				int k = i + (n-1);
				while(i<k && k<len){
					int temp = a[i];
					a[i] = a[k];
					a[k] = temp;
					k--;i++;
				}
			}
		}
	}

- anonymousNg November 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Time: O(n). Space: O(1).

public static void swap(Integer[] arr, int i, int j) {
		int tmp = arr[i];
		arr[i] = arr[j];
		arr[j] = tmp;
	}
	public static Integer[] reverse(Integer[] arr, int n) {
		int parts = arr.length/n;
		int k;
		int j;
		for(int i=0;i<parts;i++) {
			k=n-1;
			j=0;
			while(j<k) {
				if((i*n+k)>=arr.length) {
					k--;
				}else {
					swap(arr,i*n+(j++), i*n+(k--));				
				}
			}
		}
		return arr;
	}

- engeloded November 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in python one liner:

Array=[1,2,3,4,5,6,7,8,9]
 N = 3 
print Array[0:N][::-1]+Array[N:]

- Anonymous March 09, 2018 | 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