Boomerang Commerce Interview Question
Developer Program EngineersCountry: India
Interview Type: In-Person
package design;
import java.util.*;
public class DemoPhoneContact {
public static void main(String args[]){
PhoneContact phone=new PhoneContact(new HashSet());
User u1=new User("9945454545","Eshwar", "eshwar@gmail.com");
User u2=new User("8877003681", "hari", "hari@gmail.com");
User u3=new User("7788990034","prabha", "prabha@gmail.com");
User u4=new User("8989898987", "arjun", "arjun@gmail.com");
User u5=new User("9738383838", "kiran", "kiran@gmail.com");
phone.addUser(u1);
phone.addUser(u2);
phone.addUser(u3);
phone.addUser(u4);
phone.addUser(u5);
User user=phone.searchUser("eshwar@gmail.com");
if(user!=null){
System.out.println("UserDetails :: " + user.getName() +" ," + user.getEmail() + ", "+ user.getPhoneNumber());
}else {
System.out.println("Sorry number nor exist");
}
}
}
class PhoneContact {
Set<User> set;
public PhoneContact(Set<User> set){
this.set=set;
}
public void addUser(User user){
set.add(user);
}
public User searchUser(String name){
Iterator<User> itr=set.iterator();
User user=null;
while(itr.hasNext()){
User myuser=itr.next();
if(name.equalsIgnoreCase(myuser.getName())|| name.equalsIgnoreCase(myuser.getEmail())){
user=myuser;
break;
}
}
return user;
}
}
class User implements Comparable<String> {
private String phoneNumber;
private String name;
private String email;
public User(String phoneNumber, String name, String email){
this.phoneNumber=phoneNumber;
this.name=name;
this.email=email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((phoneNumber == null) ? 0 : phoneNumber.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (phoneNumber == null) {
if (other.phoneNumber != null)
return false;
} else if (!phoneNumber.equalsIgnoreCase(other.phoneNumber))
return false;
return true;
}
@Override
public int compareTo(String arg0) {
if(this.phoneNumber.compareTo(arg0)==0){
return 0;
}else {
return this.phoneNumber.compareTo(arg0);
}
}
}
We can use a Trie data structure here.
Store all the information of any user in object, lets say Contact. Now add the name of user in a Trie and nod at which name ends, store that user's object at this node and mark as a end of string.
Similarly we can add all information of that user like number, email id, company name in this Trie( yes, considering we have a space ) and store particular Conatact object at the end of every node where string ends.
Now do Trie string search and display all unique objects.
Here the question is more about how to identify multiple user records belong to same user,
- Vib February 28, 2015This is more of a idea generation and discussion type question rather than design....
Ex:
you have ("9893342233", "Eshwar", "eshwar@gmail.com" ), ("040-23554677", "Eshwar Prasad", "eshwar.prasad@work.com"),
Now identify both belong to same person and merge them / tag them to same person.