Microsoft Interview Question
Software Engineer in Testsyup 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
}
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.
Looks like the nlogn algo does not exist.
Although I did not check carefully.
http://discuss.joelonsoftware.com/default.asp?interview.11.603254.8
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 ?
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);
}
}
}
}
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.
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;
}
//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
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.
can be done in O( n log n ) by first sorting inplace and then iterate through the array once checking adjacent numbers.
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.
Can be done in O(N)
- CodeRed December 09, 2008Create 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.