Symantec Interview Question for Principal Software Engineers


Country: United States




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

public class Pets implements Comparable<Object>{

    String name;

    public Pets(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int compareTo(Object o) {
        if(o instanceof Pets){
            return name.compareTo(o.toString());
        }
        throw new RuntimeException("Only works for Pets Class.");
    }

    @Override
    public String toString() {
        return name;
    }
}

public class Cat extends Pets {

    public Cat(String name) {
        super(name);
    }
}

public class Dog extends Pets {

    public Dog(String name) {
        super(name);
    }
}

public class Test {
    public static void main(String arg[]) {
        Object[] pets = { new Dog("Max"), new Cat("Snow Ball"),
                new Dog("Brownie"), new Cat("Not so Brownie") };

        System.out.println("\nBefore Sorting:\n");
        displayHouse(pets);

        Arrays.sort(pets);

        System.out.println("\nAfter Sorting:\n");
        displayHouse(pets);
    }

    private static void displayHouse(Object[] pets) {
        for (Object pet : pets) {
            System.out.println(pet);
        }
    }
}

Before Sorting:
Max
Snow Ball
Brownie
Not so Brownie

After Sorting:
Brownie
Max
Not so Brownie
Snow Ball

- trish August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 votes

Comparable<Pets> would be better

- thelineofcode September 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it's better to implement a Pet class, and let Cat and Dog extend Pet.
implement equal for Pet

- Anonymous August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

normally sort is based on compare, rather than just check equals.
A simple implementation is define a super class for both, e.g. Pet or Animal
define a method compareTo() in it.
Cat and Dog, override compareTo method, if needed.

- Mingjun August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Depending on the parameter which are required for comparing the Objects(CAT and DOG in this case). Equals can be manipulated to see what we wish it to see:) . .


Code: -

import java.util.Arrays;

/*

Q) Sort the array(have to implement the equals method in Cat and Dog).

Cat c = new Cat("Snow Ball");
Dog d = new Dog("Max");
array = {c, d, ... };

*/
class Dog implements Comparable<Object> {
String name;

// other dog attributes

// Only Constructor
Dog(String name) {
this.name = name;
}

@Override
public boolean equals(Object otherObj) {
if (otherObj instanceof Dog || otherObj instanceof Cat) {
// can compare other common attributes if required
return name.equals(otherObj.toString());
}
return false;
}

@Override
public String toString() {
return name;
}

@Override
public int compareTo(Object otherObj) {
if (otherObj instanceof Dog || otherObj instanceof Cat) {
return name.compareTo(otherObj.toString());
}
throw new RuntimeException("Only works for Cats and Dogs Class.");
}

}

class Cat implements Comparable<Object> {
String name;

// other cat attributes

// Only Constructor
Cat(String name) {
this.name = name;
}

@Override
public boolean equals(Object otherObj) {
if (otherObj instanceof Dog || otherObj instanceof Cat) {
// can compare other common attributes if required
return name.equals(otherObj.toString());
}
return false;
}

@Override
public int compareTo(Object otherObj) {
if (otherObj instanceof Dog || otherObj instanceof Cat) {
return name.compareTo(otherObj.toString());
}
throw new RuntimeException("Only works for Cats and Dogs Class.");
}

@Override
public String toString() {
return name;
}

}

public class Main {

public static void main(String arg[]) {
Object[] pets = { new Dog("Max"), new Cat("Snow Ball"),
new Dog("Brownie"), new Cat("Not so Brownie") };

System.out.println("\nBefore Sorting:\n");
displayHouse(pets);

Arrays.sort(pets);

System.out.println("\nAfter Sorting:\n");
displayHouse(pets);
}

private static void displayHouse(Object[] pets) {
for (Object pet : pets) {
System.out.println(pet);
}
}

}

- acJavaFreak August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String[] array={a,b,...};
ArrayList alist=new ArrayList(array);
alist.sort();

- Onkar October 06, 2013 | 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