Goldman Sachs Interview Question
Software Engineer / Developersimport java.util.HashSet;
import java.util.Set;
public class MainClass {
public static void main(String args[]) {
String[] name1 = { "Amy", "Jose", "Jeremy", "Alice", "Patrick" };
String[] name2 = { "Alan", "Amy", "Jeremy", "Helen", "Alexi" };
String[] name3 = { "Adel", "Aaron", "Amy", "James", "Alice" };
Set<String> letter = new HashSet<String>();
for (int i = 0; i < name1.length; i++)
letter.add(name1[i]);
for (int j = 0; j < name2.length; j++)
letter.add(name2[j]);
for (int k = 0; k < name3.length; k++)
letter.add(name3[k]);
System.out.println(letter.size() + " letters must be sent to: " + letter);
}
}
hash is best in this case, O(n)
Insert into hash with key = number and data = key.count++, //initially count is zero
if count>1 then dont print because it is already inserted and printed.
You don't have to maintain the data( count), HashSet wont allow duplicate key anyways.
Using HashSet is good. Because creating BST will also take memory. and complexity is also n(logn).
But if you need to iterate through those unique elements then better u should use BST. Because if Hash Code is not good then HashSet can behave very badly.
You don't have to maintain the data( count), HashSet wont allow duplicate key anyways.
Using HashSet is good. Because creating BST will also take memory. and complexity is also n(logn).
But if you need to iterate through those unique elements then better u should use BST. Because if Hash Code is not good then HashSet can behave very badly.
Create a tree with these numbers and also keep the freq of each number at every node
- DashDash August 10, 2010Now do an inorder traversal to print only those with freq 1
Time complexity : nlogn