Microsoft Interview Question
Software Engineer in Testsimport java.util.*;
import java.lang.*;
class Main
{
static String[] strs = {
"arrays", "java", "manipulate", "java",
"simple", "arrays", "manipulate", "java"
};
public static void main(String[] args) {
print(strs, "strs");
int num = getNumUniqueValues();
String[] uniqueValues = new String[num];
for(int j = 0, k = 0; j < strs.length; j++) {
if(!containsValue(uniqueValues, strs[j]))
uniqueValues[k++] = strs[j];
}
print(uniqueValues, "uniqueValues");
}
private static int getNumUniqueValues() {
String[] values = new String[strs.length];
int count = 0;
for(int j = 0; j < strs.length; j++) {
if(!containsValue(values, strs[j]))
values[count++] = strs[j];
}
return count;
}
private static boolean containsValue(String[] array, String target) {
for(int j = 0; j < array.length; j++) {
if(array[j] != null && array[j].equals(target))
return true;
}
return false;
}
private static void print(String[] array, String s) {
System.out.println(s + ":");
for(int j = 0; j < array.length; j++) {
System.out.print(array[j]);
if(j < array.length-1)
System.out.print(", ");
}
System.out.println();
}
}
Gayle, i would like to modify your code, 2 methods
public static void main(String[] args) {
print(strs, "strs");
getNumUniqueValues();
}
private static void getNumUniqueValues() {
String[] values = new String[strs.length];
int count = 0;
for(int j = 0; j < strs.length; j++) {
if(!containsValue(values, strs[j]))
values[count++] = strs[j];
}
print(values, "uniqueValues");
}
there is no difference with your code except values[] array has same size as strs[] array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CsTest
{
class Program
{
static void Main(string[] args)
{
string[] arr = { "google", "name", "what", "google" };
remdup(arr);
}
static void remdup(string[] arr)
{
string[] unique = new string[arr.Length];
int i=0;
foreach (string str in arr)
{
if (!unique.Contains(str))
{
unique[i++] = str;
}
}
foreach (string str in unique)
Console.WriteLine(str);
}
}
}
The solution for an integer array
public int[] RemoveDuplicates(int[] elems)
{
HashSet<int> uniques = new HashSet<int>();
foreach (int item in elems)
uniques.Add(item);
elems = new int[uniques.Count];
int cnt = 0;
foreach (var item in uniques)
elems[cnt++] = item;
return elems;
}
Okay I got solution that takes in any array and returns it without duplicates in C# -
public T[] RemoveDuplicates<T>(T[] elems)
{
HashSet<T> uniques = new HashSet<T>();
foreach (var item in elems)
uniques.Add(item);
elems = new T[uniques.Count];
int cnt = 0;
foreach (var item in uniques)
elems[cnt++] = item;
return elems;
}
This is simple...just sort the array and remove the duplicates..O(nlogn).
Rough code:
/* returns the new size of the array*/
int remDup(int num[],int size)
{
sort(num,size); //assume sort function
int i=0,j=0;
while(i<size)
{
if(num[i]!=num[j])
{
j++;
num[j]=num[i];
}
i++;
}
return j+1;
}
<pre lang="java" line="1" title="CodeMonkey95817" class="run-this">import java.util.*;
import java.lang.*;
class Main
{
static String[] strs = {
"arrays", "java", "manipulate", "java",
"simple", "arrays", "manipulate", "java"
};
public static void main(String[] args) {
print(strs, "strs");
getNumUniqueValues();
}
private static void getNumUniqueValues() {
String[] values = new String[strs.length];
int count = 0;
for(int j = 0; j < strs.length; j++) {
if(!containsValue(values, strs[j]))
values[count++] = strs[j];
}
print(values, "uniqueValues");
}
private static boolean containsValue(String[] array, String target) {
for(int j = 0; j < array.length; j++) {
if(array[j] != null && array[j].equals(target))
return true;
}
return false;
}
private static void print(String[] array, String s) {
System.out.println(s + ":");
for(int j = 0; j < array.length; j++) {
System.out.print(array[j]);
if(j < array.length-1)
System.out.print(", ");
}
System.out.println();
}
}
</pre>
MS IDC makes fool of people.
People have to come to office on weekends due to workload and do night outs, no work life balance. They pay 10-20% more make people labour.
Do take the feedback from employees before joining MS.
And work is junk, all junk wor from Redmond is transferred to IDC. Ask any team, whether they design, implement products or just do porting or maintenance or make tools.
1> Sort and Delete .T.C=O(nlogn)
- Anonymous May 19, 2010Or
2> Map and rewrite .T.C=O(N) , S.C=O(N)