Microsoft Interview Question


Country: India
Interview Type: In-Person




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

Explaining the question further:-

Arr-3 is formed by eliminating 3rd, 6th, 9th ... (x*3)th from Arr-2
Now since Arr-2 was {1,3,5,7,9,11,13 ...} (after eliminating 2nd,4th,6th (x*2)th elements from Arr-1) -
Arr-3 eliminates 5(3rd element), 11(6th element) ... from Arr-2 and forms Arr-3={1,3,7,9,13,15 ...}

So Arr-k formed by removing x*kth element from Arr-(k-1) immediate preceding array of Arr-k.

Please revert if am still not clear.

- biraja1985 December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void checkExistense(int z, int k){
		int d = checkPrime(z);
		if(d==0){
			System.out.println(z+" is present in Arr-"+k);
			return;
		}
		if(k>d){
			System.out.println(z+" is not present in Arr-"+k);
		}else{
			if(z%2==0 || z%3==0 || z%5==0){
				System.out.println(z+" is not present in Arr-"+k);
			}else{
				System.out.println(z+" is present in Arr-"+k);
			}
		}		
	}
	
	public static int checkPrime(int z){
		for(int i=(int) Math.sqrt(z);i>1;i--){
			if(z%i==0)
				return i;
		}
		return 0;
	}

- AlgoAlgae December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

public static boolean exists(int z, int k)
{
if(k<=1) return true;
if(z<1) return false;
if(z==1) return true;
if(z<=k) return false;
for(int i=2;i<=k;i++)
{
if(z%i==0)
return false;
}
return true;
}

- Jackie December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x=z;
for(i=2; i<=k; i++)
{
if(x%i==0)
return " z exist in Arr-(k-1) ";
else
x=(x/k)*(k-1) + x%k; // used to calculate the position of z in Arr-i
}

return " z exist in Arr-k at position x";


Space Complexity: O(1)
Time Complexity: O(K)

- Ashwini Kumar December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Correcting problem in above code.....

x=z;
for(i=2; i<=k; i++)
{
if(x%i==0)
return " z exist in Arr-(i-1) ";
else
x=(x/i)*(i-1) + x%i; // used to calculate the position of z in Arr-i
}

return " z exist in Arr-k at position x";

- Ashwini Kumar December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i dont understand...whats the output for k=2,z=2 according to ur code?

- Anonymous December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

at k=2, z=2
output is:
2 exist in Arr-1

this implies it does not exist in arr-2....

or u can change the return as "false"
it depend upon u....

- Ashwini Kumar December 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is this really from msft? Anyways

for(int i=2;i<=k;i++){
  if(i>z)
    return true;
  if(z%i==0)
    return false;
  z = z - z/i; // z/i returns integer part only
}

- Sathya December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

at z=7 and k=3,
it does not give any output

- Ashwini Kumar December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey dude(satya) have you even read the question or just htting in air ........ hiihiihii

- Ayush December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

dude i forgot the final return true statement thats it. Did you even try running my code?

for(int i=2;i<=k;i++){
  if(i>z)
    return true;
  if(z%i==0)
    return false;
  z = z - z/i; // z/i returns integer part only
}
return true;

- Sathya December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain logic? is z an element of array or is it an index?

- confused_banda December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

z is an element. lets say it is 11 and k is 3. now the idea is when arr-2 is formed all numbers at indices divisible by 2 is gone. so 5 nos before 11 (11/2) gets cancelled and the new index position of 11 is 11-5=6. now when arr-3 is formed all elements at indices divisible by 3 gets cancelled hence 11 gets cancelled too.

- Sathya December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So z = z - z/i is calculating new index of z?

z%i == 0 understood, if z is at index which is multiple of i then its not present
But why returning true when (i > z)? Won't it prematurely terminate the loop and return true even if it false?

- confused_banda December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i>z returns true cos there is no need to find further arrays. Lets says z=3, k=20 now z's position has become 2 in arr-2. it will certainly be present in arr-3(i=3>z=2) in fact it will always be present in all the arrays that follows so why bother calculating them?

- Sathya December 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import math

def alg(z, k):
    for i in range(2, k+1):
        if z % i == 0:
            return False
        else:
            z = z - math.floor(z/i)
    return True


print(alg(9, 3))
print(alg(9, 4))

- Nishant Bhatt December 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

array-2 is all elements of array except multiples of 2
array-3 is all elements of array except multiples of 2, 3
array-4 is all elements of array except multiples of 2,3,4
and .... so on
array-k is all elements of array except multiples of 2,3,4,5,....k-1

For each number i from 2 to k, if z is multiple of i, a number z can not be in array.
z can be a multiple of number from 2 to k, if its multiple of prime between 2 to k
Find out prime numbers between 2 to k (O(k) time and space complexity).
Check if z is multiple of prime numbers between 2 to k (O(k) time complexity)
If it is then z can't be in array-k
If its guaranteed that z is always taken from array then we end here otherwise then run binary search(O(logn) time complexity) if array is sorted (else linear search O(n) or sort and search(O(logn)) to find z in array. if found z is in array-k, else it is not in array-k

- confused_banda December 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wrong. checkout array 3 has 9

- Sathya December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That array-3 is wrong. How can array-3 have 9 when array-3 is formed by removing all elements from array-2 which are of form x*3 (where x belongs to Natural Numbers)?
Given Arr3 has following elements
Arr3={1,3,7,9,13,1519,21,25,27 ... }
How is 11, 17 are removed? are they of form x*3?

- confused_banda December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok the post was unclear if x is an INDEX into an array or an element. I stand corrected.

- confused_banda December 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually arrayk will not have multiples of 2,2+3,2+3+4,.....2+3+4+....+k

- Anonymous December 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wrong again. See arr-3 has 15

- Anonymous December 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static bool Exists(int arg, int k)
        {
            for (int mod = 2; mod <= k && mod <= arg; mod++)
            {
                if (arg % mod == 0) return false; else arg -= (arg / mod);
            }
            return true;
        }

- Flux December 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean exists(int number,int k) {
        for(int i = 2;i <= k && i <= number;i++) {
            if(number % i == 0) {
                return false;
            } else {
                number -= (number / i);
            }
        }
        return true;
    }

- glebstepanov1992 December 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Arr-k is formed by eliminating x*k elements from Arr-(k-1),NOT Arr-1。
I dont think anyone really undertand what it means, except the author.

- busycai December 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Say if we are to check for value N in round K, then we check from 1st round till Kth round. At each round we are interested in the index of N.
- Clearly when K=2 index of N is N itself (as nothing has been eliminated yet)
- When K=3 then index of N is N - (floor(N/2)); as floor(N/2) elements have been eliminated when K=2

Also in each round (say K) a number whose index is 'X' is eliminated if X%K=0
Therefore we start from 1st round and continue up and see if the number survives till Kth round.

bool isPresent(int N, int K) {
 int curRound=2, curIndex=N;
 while (curRound < K) {
   if (curIndex%curRound==0) return false;
   curIndex = curIndex - (floor(curIndex/curRound));
   curRound++;
 }
 return true
}

Time complexity: O(K)
Space complexity: O(1)

- IntwPrep.MS December 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

So for this if k = 1 then it exists always else for z to exist it should not be divisible by 2 and 2+3 and 2+3+4 and.........2+3+4+....+k.

public static void main (String[] args) throws java.lang.Exception
	{
		Scanner inp = new Scanner(System.in);
		int k = inp.nextInt();
		int z = inp.nextInt();
		if(existsnum(k,z)) 
			System.out.println("Yes it exists");
		else
			System.out.println("No it doesn't");
	}
	public static boolean existsnum(int k, int z){
		if(k==1)	return true;
		int temp = 2;
		for(int i=2;i<=k;i++){
			if(z%temp==0)	return false;
			temp+= i+1;
			
		}
		return true;
	}

- Gaurav Mishra December 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Time complexity O(k)
Space O(1)

- Anonymous December 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You need to check only for primes in 2 to k..You can use sieve method for that.
So space O(k) time O(kloglogk)..
However, if you check for every k, space=O(1), time is O(k). I don't undderstand which one of the two is better? Is mod operation costly so that we should use the first way, or should we go with the basic intuition of the second method?

- alex December 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public bool ElementExists(int[] array,int z, int k)
{
int index = -1;

for (int i = 0; i < array.Length; i++)
{
if (array[i] == z) { index = i; break; }
}


if (index == 1) return true;

for (int i = 2; i <= k; i++)
{
if (index % i == 0) return false;
index = ((i-1)*(index/i))+(index%i);
}

return true;
}

- Tanuja December 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public bool ElementExists(int[] array,int z, int k)
{
int index = -1;

for (int i = 0; i < array.Length; i++)
{
if (array[i] == z) { index = i; break; }
}


if (index == 1) return true;

for (int i = 2; i <= k; i++)
{
if (index % i == 0) return false;
index = ((i-1)*(index/i))+(index%i);
}

return true;
}

- Tanuja December 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrayK {

    public static void main(String[] args){

        int z=5;
        int index=z;
        int k=3;
        int i;

        for(i=2;i<=k;i++){

            if(index%i !=0) {

                if(i==k){

                    System.out.print("the number with value  " + z +"   is present in Array " + k);
                }

            index = index-(index/i);

            }

            else

            {
                System.out.print("the number with value  " + z +"   is not present in Array " + k);

                break;
            }


        }




    }

- megha December 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isPresentInArray_K(int number, int k)
	{
		//base conditions
		if(k == 1)
			return true;
		
		if(k >= 2 && (number % 2) == 0)
			return false;
				
		// approach for the Array3 and after
		else
		{
			int positionInArrayTwo = (int) Math.ceil((double)number/2);
			System.out.println("The position of " + number + " in Array_2 is : " + positionInArrayTwo);
			
			int positionInPreviousArray = positionInArrayTwo;
			int positionInCurrentArray;
			
			for(int i = 3 ; i <= k ; i++)
			{
				if(positionInPreviousArray % i == 0)
					return false;
				
				positionInCurrentArray = positionInPreviousArray - (positionInPreviousArray / i);
				System.out.println("The position of " + number + " in Array_" + i + " is : " + positionInCurrentArray);
				positionInPreviousArray = positionInCurrentArray;
			}
			return true;

}

- Amit Kumar Yadav January 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 . Z cannot be multiple of any number between [2,k].
2. Z can be any prime number <= N
3. Z can be any number which is multiple of [k+1,N].

- NaMo January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Test.cpp : main project file.

#include "stdafx.h"
#include <iostream>

using namespace System;

#define MAX_DEPTH_INPUT 100

bool test(int k, int z)
{
    //could use alloc to cover more than the limit
    if (k > MAX_DEPTH_INPUT) return false;
    int inc[MAX_DEPTH_INPUT] = {0};

    if (k == 0 || z <= 0) return false;
    if (k == 1 || z == 1) return true;

    //Gen the increament, 2, 4, 6, 2, 4, 6 for depth 4, etc.
    for (int i=0; i<k-1; i++)
    {        
        inc[i] = 2 * ((i % (k-1)) + 1);
    }

    //Test
    bool result = false;
    int sum = 1;
    int add = 0;

    while (sum <= z)
    {
        sum += inc[add % (k-1)];
        if (sum == z) result = true;
        add++;
    }

    return result;
}

int main(array<System::String ^> ^args)
{
    int k = 2;
    int z = 2;

    for (k=0; k<10; k++){
        for (z = 0; z<100; z++){
            printf("K: %d, Z: %d, Result: %d\n", k, z, test (k, z));
        }
    }

    return 0;
}

- J99 April 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I don't know if I have understood the question properly......also feel should be Arr3={1,2,4,5,7,10,11,13,14,16,17,19,.....} is what understood from explaination of Arr2. If this true then

boolean isValueExistInArr=false;
if (z%k==0){
   isValueExistInArr=true;
}else{
isValueExistInArr=false;
}

- Arif-Guest December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope arr3 is formed from Arr2 and the condition not from Arr1

- Gaurav Mishra December 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
int arr[50]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
int i,j;

printf("Array elements are:-");
for(i=0; i<25; i++)
printf("%5d", arr[i]);

printf("\n");

int Array_kth, No;
printf("Enter the kth Array and No to find in Kth array:-");
scanf("%d %d", &Array_kth, &No);

bool found = false;
if( Array_kth==1 )
{
printf("Found the Element in first array\n");
return 0;
}

for(i=1; i<=No; )
{
if(i==No)
{
found = true;
break;
}
for(j=1; j<Array_kth; j++)
{
if(i==No)
{
found = true;
break;
}
i=i+(j*2);
}

if(found)
break;
}

if(found)
printf("Number %d Found the Element in array %d\n", No, Array_kth);
else
printf("Number %d Not Found the Element in array %d\n", No, Array_kth);

printf("\n");

return 0;
}

- Eptakhar Khan December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

System.out.println("");

- vivek December 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

public static void main(String[] args) {

		int n = 100;
		int z = 39;
		int k = 7;
		int indexToRemove = 2;
		Integer[] arr = new Integer[n];
		for (int i = 1; i <= n; i++) {
			arr[i - 1] = i;
		}
		Queue<Integer> q = new LinkedList<Integer>(Arrays.asList(arr));
		for (int i = 0; i < k - 1 && !q.isEmpty(); i++) {
			int counter = 0;
			int initialSize = q.size();
			while (counter < initialSize) {
				for (int j = 0; j < indexToRemove - 1 && counter < initialSize; j++, counter++) {
					q.offer(q.poll());
				}
				if (counter < initialSize) {
					q.poll();
					counter++;
				}
			}
			indexToRemove++;
		}

		System.out.println("Element exists: " + q.contains(z));

	}

- thelineofcode December 16, 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