Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
This is more of a database schema question, wherein subordinates of a employee is represented as foreign key in the employee table.
Representing in OOP model,
Every employee can have a vector to store references of his subordinates, the only issue with this, to store references of all the subordinates in a vector of employee class it needs all subordinate objects must have been instantiated.
Data base system are designed to manage large bodies of information in addition the database system must a sure the safty of the information stred and protect them from unauthrised acces
We can do it by using column constraint in the same table as manager, subordinate will be in same table. In Employee table we will have "MANAGER_ID"(this is manager's id for this employee row) column which will have constraint on "ID" column.
We can do it by using column constraint in the same table as manager, subordinate will be in same table. In Employee table we will have "MANAGER_ID"(this is manager's id for this employee row) column which will have constraint on "ID" column.
We can do it by using column constraint in the same table as manager, subordinate will be in same table. In Employee table we will have "MANAGER_ID"(this is manager's id for this employee row) column which will have constraint on "ID" column.
We can do it by using column constraint in the same table as manager, subordinate will be in same table. In Employee table we will have "MANAGER_ID"(this is manager's id for this employee row) column which will have constraint on "ID" column.
We can do it by using column constraint in the same table as manager, subordinate will be in same table. In Employee table we will have "MANAGER_ID"(this is manager's id for this employee row) column which will have constraint on "ID" column.
We can do it by using column constraint in the same table as manager, subordinate will be in same table. In Employee table we will have "MANAGER_ID"(this is manager's id for this employee row) column which will have constraint on "ID" column.
We can do it by using column constraint in the same table as manager, subordinate will be in same table. In Employee table we will have "MANAGER_ID"(this is manager's id for this employee row) column which will have constraint on "ID" column.
We can do it by using column constraint in the same table as manager, subordinate will be in same table. In Employee table we will have "MANAGER_ID"(this is manager's id for this employee row) column which will have constraint on "ID" column.
We can do it by using column constraint in the same table as manager, subordinate will be in same table. In Employee table we will have "MANAGER_ID"(this is manager's id for this employee row) column which will have constraint on "ID" column.
We can do it by using column constraint in the same table as manager, subordinate will be in same table. In Employee table we will have "MANAGER_ID"(this is manager's id for this employee row) column which will have constraint on "ID" column.
package emp;
import java.util.HashSet;
import java.util.Set;
public class Employee {
int id;
String firstName;
String lastName;
Employee manager;
Set subordinates = new HashSet<Employee>();
Employee(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
void addManager(Employee manager) {
this.manager = manager;
manager.subordinates.add(this);
}
void addSubordinate(Employee subordinate) {
this.subordinates.add(subordinate);
subordinate.manager = this;
}
void findDetails() {
if (this.manager != null) {
System.out.println("Manager is : " + this.manager);
} else {
System.out.println("NO manager for " + this);
}
if (this.subordinates.size() > 0) {
System.out.println("***SUB ORDINATES***");
for (Object subordinate : this.subordinates) {
Employee emp = (Employee) subordinate;
System.out.println(emp);
}
} else {
System.out.println("No subordinates for : " + this);
}
}
@Override
public String toString() {
return this.id + " : " + this.firstName + "." + this.lastName;
}
public static void main(String argc[]) {
Employee a = new Employee(1, "a", "aa");
Employee b = new Employee(2, "b", "bb");
Employee c = new Employee(3, "c", "cc");
Employee d = new Employee(4, "d", "dd");
Employee e = new Employee(5, "e", "ee");
Employee f = new Employee(6, "f", "ff");
Employee g = new Employee(7, "g", "gg");
a.addSubordinate(b);
a.addSubordinate(c);
b.addSubordinate(e);
b.addSubordinate(f);
c.addSubordinate(g);
a.findDetails();
}
}
import java.util.HashSet;
import java.util.Set;
public class Employee {
int id;
String firstName;
String lastName;
Employee manager;
Set subordinates = new HashSet<Employee>();
Employee(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
void addManager(Employee manager) {
this.manager = manager;
manager.subordinates.add(this);
}
void addSubordinate(Employee subordinate) {
this.subordinates.add(subordinate);
subordinate.manager = this;
}
void findDetails() {
if (this.manager != null) {
System.out.println("Manager is : " + this.manager);
} else {
System.out.println("NO manager for " + this);
}
if (this.subordinates.size() > 0) {
System.out.println("***SUB ORDINATES***");
for (Object subordinate : this.subordinates) {
Employee emp = (Employee) subordinate;
System.out.println(emp);
}
} else {
System.out.println("No subordinates for : " + this);
}
}
@Override
public String toString() {
return this.id + " : " + this.firstName + "." + this.lastName;
}
public static void main(String argc[]) {
Employee a = new Employee(1, "a", "aa");
Employee b = new Employee(2, "b", "bb");
Employee c = new Employee(3, "c", "cc");
Employee d = new Employee(4, "d", "dd");
Employee e = new Employee(5, "e", "ee");
Employee f = new Employee(6, "f", "ff");
Employee g = new Employee(7, "g", "gg");
a.addSubordinate(b);
a.addSubordinate(c);
b.addSubordinate(e);
b.addSubordinate(f);
c.addSubordinate(g);
a.findDetails();
}
}
How about this.. any thoughts??????
import java.util.ArrayList;
public class Employee {
static ArrayList<Employee> al = new ArrayList();
private String id, fname, lname, mgrid;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getMgrid() {
return mgrid;
}
public void setMgrid(String mgrid) {
this.mgrid = mgrid;
}
public static void main(String[] args) {
// Loading the employees
Employee vo = new Employee();
vo.setFname("EmpFname");
vo.setLname("EmpLname");
vo.setId("1");
vo.setMgrid("100");
al.add(vo);
Employee vo1 = new Employee();
vo1.setFname("EmpFname2");
vo1.setLname("EmpLname2");
vo1.setId("3");
vo1.setMgrid("100");
al.add(vo1);
Employee vo2 = new Employee();
vo2.setFname("EmpLname3");
vo2.setLname("EmpLname3");
vo2.setId("4");
vo2.setMgrid("101");
al.add(vo2);
Employee vo3 = new Employee();
vo3.setFname("EmpLname4");
vo3.setLname("EmpLname4");
vo3.setId("6");
vo3.setMgrid("101");
al.add(vo3);
Employee vo4 = new Employee();
vo4.setFname("EmpLnameMgr");
vo4.setLname("EmpLnameMgr");
vo4.setId("100");
vo4.setMgrid("1000");
al.add(vo4);
showSubordinates("100");// this method shows the subordinates for Mgr ID
// which is passed.
}
public static void showSubordinates(String EmpID) {
boolean flg = false;
for (int i = 0; i < al.size(); i++) {
Employee vo = al.get(i);
if (vo.getMgrid().equals(EmpID)) {
flg = true;
System.out.println("Subordinate for Mgr " + EmpID + " is: "
+ vo.getFname() + ", " + vo.getLname() + ", EmpID: "
+ vo.getId());
}
}
if (!flg) {
System.out.println("No Subordinates found");
}
}
}
- Anonymous April 09, 2012