Amazon Interview Question
SDE-2sCountry: United States
import java.util.Set;
import java.util.TreeSet;
public class StudentSort {
public static void main(String a[])
{
Set<Student> studentList=new TreeSet<Student>();
studentList.add(new Student("r",1));
studentList.add(new Student("a",1));
studentList.add(new Student("p",1));
studentList.add(new Student("t",2));
studentList.add(new Student("k",3));
studentList.add(new Student("b",1));
studentList.add(new Student("a",2));
studentList.add(new Student("e",3));
for(Student obj : studentList)
{
System.out.println("Grade : "+obj.getStudentGrade()+" Name : "+obj.getStudentName());
}
}
static class Student implements Comparable<Student>
{
String studentName;
Integer studentGrade;
Student(String name, Integer grade)
{
studentName=name;
studentGrade=grade;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentGrade() {
return studentGrade;
}
public void setStudentGrade(Integer studentGrade) {
this.studentGrade = studentGrade;
}
@Override
public int compareTo(Student obj) {
if(this.getStudentGrade().compareTo(obj.getStudentGrade())==0)
{
return this.getStudentName().compareTo(obj.getStudentName());
}
else
{
return this.getStudentGrade().compareTo(obj.getStudentGrade());
}
}
}
}
How about as following -
Student.java
package com.amazon.random.studentList;
public class Student implements Comparable<Student> {
private String name;
private Grade grade;
public Student(){
}
public Student(String name, Grade grade){
this.name = name;
this.grade = grade;
}
public enum Grade {
a,b,c,d;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
@Override
public int compareTo(Student o) {
if(this.getGrade().ordinal() == o.getGrade().ordinal())
return (this.getName().compareTo(o.getName()));
else
return (this.getGrade().ordinal() - o.getGrade().ordinal());
}
}
ListOfStudentsQ.java
package com.amazon.random.studentList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class ListOfStudentsQ {
private List<Student> studentList;
public ListOfStudentsQ(){
studentList = new LinkedList<Student>();
generateList();
}
public void generateList(){
studentList.add(new Student("q",Student.Grade.b));
studentList.add(new Student("p",Student.Grade.b));
studentList.add(new Student("r",Student.Grade.a));
studentList.add(new Student("s",Student.Grade.c));
Collections.sort(studentList);
}
public void displayList(){
Iterator<Student> iter = studentList.iterator();
while(iter.hasNext()){
System.out.println("Student Name According to Grade: " + iter.next().getName());
}
}
public static void main(String args[]){
ListOfStudentsQ obj = new ListOfStudentsQ();
obj.displayList();
}
}
Can't we just implement it just using dutch flag algorithm ..... we have only four grades,
1. Create four lists for each grade.
A for grade a
B for grade b
C for grade c
d for grade d
2. Scan already sorted list and check grade of each student, add it to corresponding linked list.
3. At last add all lists - A->B->C->D
Time complexity: O(N)
Since the question mentions that the students are already sorted by name, it's enough to just do a second sort pass using a stable sorting algorithm like merge sort.
- Ivan June 06, 2014