Symantec Interview Question
Java DevelopersCountry: India
The problem is not clear. But if your issue is to create a unique hashCode when the first name and last name change you can do like this:
class Employee{
int age;
String fname;
String lname;
public int hasCode(){
if(fname.hashCode()<lname.hashCode())
return (fname+lname).hashCode();
return (lname+fname).hashCode();
}
}
If you check hashcode and equals method hashcode method will return same hashcode for "abc" and "cba" and these two strings will be different when equals method will get executed so if we take fName+lName and lName+fName method into account these two methods will be same as per hashcode but we need to provide implementation of equals method in such a way that it will return true even if hashcode is equal so that we will get the same object
Um, O(1) refers to a constant time complexity. By the rules of big-O notation arithmetics your O(2) = O(2*1) = O(c*1) = O(1)
Hi,
We could introduce a key by sorting the characters of first and last names in alphabetical order (i.e a mangled name as a key)
signature (first, last)
{
sort(first, last)
}
employees[signature(first,last)] = employee
And obviously, when we look up, we got to construct the unique signature again using first and last names and will end up with just one access using the key
I hope this makes sense
Thanks
Guess interviewer's brand is johnny walker,
Can't we just store both lastname -> firsname and firstname -> lastname in the same map and do with 1 comparision ? you see any issue ?
I tried two answers but both were rejected
1. storing firstname+lastname and lastname+firstname in hashmap: will consume extra space.
2. tried something similar to @Karthikeyan's approach, creating unique key using characters, by adding up the ascii values. But the scenario failed. The following example will explain:
Employee 1:
First name : Abc
Last name: Def
Employee 2:
First name: Dbc
Last name: Aef
Both employees will have same ascii value sum, and on sorting, will yield the same result.
Create a new class with two String member variables "firstname" and "lastname".
Overwrite hashCode() with the firstname.hashCodexlastName.hashCode.
equals method should be overriden for both variation of fname+lName and lName+fName. In one word equality should be defined in such a way.
This object will be Key. Employee object will be value in the HashMap.
While searching create a new object with the fname+lName (or reverse) and find in the HashMap.
But Hash function should be more optimized.
How about building tries for both first name's and last name's of employees. If you need prefix searching for every characters.
I think but your problem is about tokenizing the search string based on separator and comparing each token with firstname and lastname of employee list, if found break the loop.
If you check hashcode and equals method hashcode method will return same hashcode for "abc" and "cba" and these two strings will be different when equals method will get executed so if we take fName+lName and lName+fName method into account these two methods will be same as per hashcode but we need to provide implementation of equals method in such a way that it will return true even if hashcode is equal so that we will get the same object
class Employee{
String fName;
String lName;
public int hashCode(){
return (fname+lName).hashCode();
}
If you check hashcode and equals method hashcode method will return same hashcode for "abc" and "cba" and these two strings will be different when equals method will get executed so if we take fName+lName and lName+fName method into account these two methods will be same as per hashcode but we need to provide implementation of equals method in such a way that it will return true even if hashcode is equal so that we will get the same object
}
If you check hashcode and equals method hashcode method will return same hashcode for "abc" and "cba" and these two strings will be different when equals method will get executed so if we take fName+lName and lName+fName method into account these two methods will be same as per hashcode but we need to provide implementation of equals method in such a way that it will return true even if hashcode is equal so that we will get the same object
If you check hashcode and equals method hashcode method will return same hashcode for "abc" and "cba" and these two strings will be different when equals method will get executed so if we take fName+lName and lName+fName method into account these two methods will be same as per hashcode but we need to provide implementation of equals method in such a way that it will return true even if hashcode is equal so that we will get the same object
If you check hashcode and equals method hashcode method will return same hashcode for "abc" and "cba" and these two strings will be different when equals method will get executed so if we take fName+lName and lName+fName method into account these two methods will be same as per hashcode but we need to provide implementation of equals method in such a way that it will return true even if hashcode is equal so that we will get the same object
Override the hascode method of Employee and use the employee object hashcode as a key in hasmap:
package inheritance;
import java.util.HashMap;
/**
* Created by HP PC on 4/17/2016.
*/
public class Employee {
public void setFname(String fname) {
this.fname = fname;
}
public void setLname(String lname) {
this.lname = lname;
}
String fname;
String lname;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
if (!fname.equals(employee.fname)) return false;
return lname.equals(employee.lname);
}
@Override
public int hashCode() {
int result = fname.hashCode();
result = 31 * result + lname.hashCode();
return result;
}
Employee(String fname , String lname){
this.fname = fname;
this.lname = lname;
}
public static void main(String[] args) {
Employee employee1 = new Employee( "Fname", "abc");
Employee employee2 = new Employee( "Fname", "bac");
Employee employee3 = new Employee( "Fname", "Abc");
HashMap<Integer, Employee> refMap = new HashMap<Integer, Employee>();
refMap.put( employee1.hashCode() , employee1);
refMap.put(employee2.hashCode(), employee2);
refMap.put(employee3.hashCode(), employee3);
//create object from ui input
Employee employee4 = new Employee( "Fname", "bac");
//search 1
Employee emp = refMap.get(employee4.hashCode());
System.out.println(emp.fname + emp.lname);
// search 2
employee4.setFname("Fname");
employee4.setLname("Abc");
Employee emp1= refMap.get(employee4.hashCode());
System.out.println(emp1.fname + emp1.lname);
}
}
package inheritance;
import java.util.HashMap;
/**
* Created by HP PC on 4/17/2016.
*/
public class Employee {
public void setFname(String fname) {
this.fname = fname;
}
public void setLname(String lname) {
this.lname = lname;
}
String fname;
String lname;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
if (!fname.equals(employee.fname)) return false;
return lname.equals(employee.lname);
}
@Override
public int hashCode() {
int result = fname.hashCode();
result = 31 * result + lname.hashCode();
return result;
}
Employee(String fname , String lname){
this.fname = fname;
this.lname = lname;
}
public static void main(String[] args) {
Employee employee1 = new Employee( "Fname", "abc");
Employee employee2 = new Employee( "Fname", "bac");
Employee employee3 = new Employee( "Fname", "Abc");
HashMap<Integer, Employee> refMap = new HashMap<Integer, Employee>();
refMap.put( employee1.hashCode() , employee1);
refMap.put(employee2.hashCode(), employee2);
refMap.put(employee3.hashCode(), employee3);
//create object from ui input
Employee employee4 = new Employee( "Fname", "bac");
//search 1
Employee emp = refMap.get(employee4.hashCode());
System.out.println(emp.fname + emp.lname);
// search 2
employee4.setFname("Fname");
employee4.setLname("Abc");
Employee emp1= refMap.get(employee4.hashCode());
System.out.println(emp1.fname + emp1.lname);
}
}
public class Employee {
public void setFname(String fname) {
this.fname = fname;
}
public void setLname(String lname) {
this.lname = lname;
}
String fname;
String lname;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
if (!fname.equals(employee.fname)) return false;
return lname.equals(employee.lname);
}
@Override
public int hashCode() {
int result = fname.hashCode();
result = 31 * result + lname.hashCode();
return result;
}
Employee(String fname , String lname){
this.fname = fname;
this.lname = lname;
}
public static void main(String[] args) {
Employee employee1 = new Employee( "Fname", "abc");
Employee employee2 = new Employee( "Fname", "bac");
Employee employee3 = new Employee( "Fname", "Abc");
HashMap<Integer, Employee> refMap = new HashMap<Integer, Employee>();
refMap.put( employee1.hashCode() , employee1);
refMap.put(employee2.hashCode(), employee2);
refMap.put(employee3.hashCode(), employee3);
//create object from ui input
Employee employee4 = new Employee( "Fname", "bac");
//search 1
Employee emp = refMap.get(employee4.hashCode());
System.out.println(emp.fname + emp.lname);
// search 2
employee4.setFname("Fname");
employee4.setLname("Abc");
Employee emp1= refMap.get(employee4.hashCode());
System.out.println(emp1.fname + emp1.lname);
}
}
seriously, Is there an O(2) ?
- bharat Desh Pandi April 21, 2015