Microsoft Interview Question for Software Engineer in Tests






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

Can be done in O(N)

Create a Hashtable, traverse the array from o to end, if an entry does not exist, create an entry in the hashtable, if it already exists, thats the first repeated number. If we know the bounds (of the elements and not the array) we can use an array instead of a hashtable.

- CodeRed December 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes
yup exactly.. {{{ #include <map> int findrepetedint(int arr){ map<int,char> match;//second arg does not matter for(int i=0;i<arr.size();i++){ if(match.find(arr[i]) != match.end()){//kind of verbose but I don't know how to search better in a map match.insert(pair<int,char>(arr[i],'1')); else return arr[i]; } return -1; corresponds to some error code } - prolificcoder April 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yup exactly..

#include <map>
int findrepetedint(int arr){
map<int,char> match;//second arg does not matter
for(int i=0;i<arr.size();i++){
if(match.find(arr[i]) != match.end()){//kind of verbose but I don't know how to search better in a map
match.insert(pair<int,char>(arr[i],'1'));
else
return arr[i];
}
return -1; corresponds to some error code
}

- prolificcoder April 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

2 different elements can have the same hash entry, isn't it?
(imagine using modulo hash functions)

- anon October 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

If extra space is allowed, hashtable is good choice

- Anonymous October 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

Another solution is creating binary search tree. it takes nlogn for creation. Now while inserting data we check if value is <,> or =. Node which satisfy first = condition is our first repetitive node.

- bhumij November 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you are anyways using extra space, why not hash table which will give u O(1) for inserting and checking for duplicates.

- sigkill July 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

any conditions for this???
random number or the value in a fixed scope?
can we use extra space?
please say it clearlly

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

If you can create an array to save the sorted array, then you can achiev O(NlgN).

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

hash table ..

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

Looks like the nlogn algo does not exist.
Although I did not check carefully.
http://discuss.joelonsoftware.com/default.asp?interview.11.603254.8

- XYZ November 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, wrong post.

- XYZ November 21, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about using bucket?

- Naren June 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if can, use hashtable O(n)

if can't, use avl tree O(nlogn)

- luna July 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. known array- A, use array B for storing the count of the numbers
2. B has indices from 0 to k where k is the largest element in array --O(n)
3. while incrementing count in B check if it reaching 2.
4. the index of the first occurence of 2 gives the repeated element--O(n)

- anon October 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ur solution is not considering the space, which also needs to be considered here .. what will happen if the array has an element 999999 etc , can we have the second array B that much size ?

- Anonymous November 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Absolutely correct. This seems to be the best solution

- vivekh October 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Build a BST with the given array. The first repeated element can be caught while building the BST itself (if we are trying to insert a value equal to its parent). Thus just takes O(logn).

- Helper April 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a fucking Hashset

- Anonymous December 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

'

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

and 1=

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

' and 1=

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

\'

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

'''

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

ookjk85h74

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

1 OR 1=1

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

1' OR '1'='1

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

1'1

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

' 1 AND 1=1

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

1 AND 1=1

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

1\'1

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

) or ('1'='1--

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

' or 1=1/*

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

' or 1=1--

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

order by 1000/*

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

order by 1000;--

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

' order by 1000/*

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

' order by 1000;--

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

' or 1=1--

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

" or 1=1--

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

') or ('a'='a

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

public int findFistDup(int[] a)
{
int N = a.length;
int maxA = a[0];
for (int i =0;i<N;i++)
if(a[i]>maxA) maxA = a[i];
int[] count = new int[maxA+1];
int i;
for (i=0;i<N;i++)
{
count[a[i]]++;
if(count[a[i]]>1) break;
}
return a[i];
}

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

import java.util.HashMap;


public class FirstRepeat {


public static void main(String args[])
{


int[] a= {10,23,44,23,44,23,12,3,18,19};
findfirstrepeated(a);





}

private static void findfirstrepeated(int[] a) {
// TODO Auto-generated method stub


HashMap<Integer,Integer> h= new HashMap<Integer,Integer>();

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

if(h.containsKey(x))
{
h.put( x, h.get(x)+1 );

System.out.println("The first repeated is"+x );
break;

}
else
{

h.put(x, 1);
}


}


}


}

- Prajakta May 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If Hash Table is used then the time complexity of the solution is O(n).
However, space complexity will also be O(n).

Else, we have to check by brute force and that solution is O(n ^ 2).

If we use sorting then also we need extra O(n) space, making the hash table far more easy than rest of the solutions.

- sid1505 September 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One of the easiest ways to find the first repeated element in the array is to use an unordered set and keep inserting the element in the set until you find the repeated element which is from before present in the set, if present then return the number this will be your first repeated element.

Implementation:

#include<bits/stdc++.h>
using namespace std;
int findrepeatedelement(int arr[], int n){
unordered_set<int> s;
for(int i = 0; i < n; i++){
if(s.find(arr[i]) != s.end())
return arr[i];
else
s.insert(arr[i]);
}
}
int main(){
int arr[] = {10,23,44,23,44,23,12,3,18,19};
cout<<findrepeatedelement(arr, 10);
return 0;
}

- swapnilkant11 June 06, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

//Bruteforce - O(n^2) Complexity.

#include<stdio.h>
#include<stdbool.h>

int main()
{

int inPt[10];
int i,j;
bool found = false;

printf("Give Input Number \n");

for(i=0;i<10;i++)
scanf("%d",&inPt[i]);

for(i=0;i<10;i++)
{
for(j=1;j<10;j++)
{
if(inPt[i]==inPt[j])
{ //for2 starts here
printf("The Repeated Number is %d", inPt[i]);
found = true;
break;
} //End of if
} // End of for2
if(found)
break;
}

return 0;

} // End of main

- Nachiketha October 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why would you even bother posting this?

- Anonymous January 21, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wtf???
ppl posting so trivial brute force

- Anonymous January 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Okay, I think we can solve it as contruct a empty set. Now for each iteration of the input Data Structure, count if the number of elements is equal to the value of iteration. The instance the value is not equal, the element at that position is first repeated element.

- Rahul October 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

when you have to add to the empty set you have to check if it is already present. thats like checking the original array itself.. its o(n^2) and u have created an additional array

- Anonymous July 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

can be done in O( n log n ) by first sorting inplace and then iterate through the array once checking adjacent numbers.

- Nitesh October 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope, you would lost the order by sorting. It asks for the first repeated number.

- Anonymous October 30, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ok...modify that. After sorting(O(nlogn)), find all the number that appear more than once(O(n). Then with the original array find the first number which is in the list of duplicates we found(O(n)).


finally O(nlogn) time and O(n) space.

Now I lose when someone is coming up with a question regarding array size being in millions etc.

- peace November 12, 2009 | Flag


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