Symantec Interview Question for Java Developers


Country: India




Comment hidden because of low score. Click to expand.
1
of 1 vote

seriously, Is there an O(2) ?

- bharat Desh Pandi April 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

My mistake. It is O(1). The interviewer was looking for a solution with one comparison only.

- angshu1986 April 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

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();
	}
}

- amirtar April 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you please explain how it will lead toO(1) access

- yogi.rulzz April 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous July 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)

- Anonymous April 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It will be O(1). What I meant is the number of comparisions.

- angshu1986 April 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Karthikeyan May 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 ?

- Vib May 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- angshu1986 May 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- s100banerjee May 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

seem to be easy. pros:gets result in O(1), cons: use more memory.
just use hashmap and place into it two keys (fname+lname; lname+fname). both pointing to the same object employee.

- eugene May 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Anukalp Katyal June 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous July 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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
}

- Anonymous July 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- mahendra tonape July 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- mahendra tonape July 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- mahendra tonape July 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);



}



}

- Indu May 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);



    }



   }

- Indu May 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);

    }



   }

- Indu May 22, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More