Amazon Interview Question for SDE-2s


Country: United States




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

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 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

At Least someone understood the question.
why do people make things overly complex! the question was clearly
"Implement a stable sort algorithm" That's it. Merge sort is a good one and easy to implement.

- byteattack June 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous June 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Striker June 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Selection sort is a better choice than bucket sort. Bucket sort needs extra space and then you need to merge them. Selection sort can be done in place using three pointers for the three grades.

- Manjeet June 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

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)

- Ajeet June 06, 2014 | 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