Lunatic Server Solutions Interview Question for Developer Program Engineers


Country: United States




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

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class Platter{

static int globalTime = 1;
static boolean moveForward = true;
static int head = 1;
public static void main(String[] args) {
int numberOfPlatters = 3;
int finalTime = 53;
List<Request> inputList = new ArrayList<Request>();
inputList.add(new Request(2,3,50));
inputList.add(new Request(51,1,2));
inputList.add(new Request(1,1,3));
inputList.add(new Request(51,1,2));
inputList.add(new Request(1,2,80));
inputList.add(new Request(2,2,5));
inputList.add(new Request(2,2,1));
Map<Integer, Integer> resultSet = new HashMap<Integer, Integer>();
List<Request> requestList = new ArrayList<Request>();
Collections.sort(inputList, new Comparator<Request>() {

@Override
public int compare(Request o1, Request o2) {
return o1.timeOfArrival - o2.timeOfArrival;
}

});
for (int j=1; j <= finalTime; j++) {
if (!inputList.isEmpty() && finalTime >= inputList.get(0).timeOfArrival ){
Request first = inputList.remove(0);
Platter.globalTime = first.timeOfArrival;
boolean requestProcessed = false;
requestList.add(first);
while (!requestProcessed && !inputList.isEmpty()) {
Request req = inputList.get(0);
if (req.timeOfArrival <= globalTime) {
requestList.add(inputList.remove(0));
} else {
requestProcessed = true;
}
}
}
for(Iterator <Request> iter =requestList.iterator();iter.hasNext();) {
Request request = iter.next();
if (request.trackNumber == head) {
Integer plat = resultSet.get(request.platterNumber);
if (plat==null) {
plat = 1;
} else {
plat++;
}
resultSet.put(request.platterNumber, plat);
iter.remove();
}
}
int trackNumber = 0;
for (Request request : requestList) {
int minRequest = 9999;
if (Math.abs(Platter.head- request.trackNumber) < minRequest) {
minRequest = Math.abs(Platter.head-request.trackNumber);
trackNumber = request.trackNumber;
}
if (Math.abs(Platter.head-request.trackNumber) == minRequest) {
if ((moveForward && Platter.head < request.trackNumber) || (!moveForward && Platter.head > request.trackNumber)) {
minRequest = Math.abs(Platter.globalTime-request.trackNumber);
trackNumber = request.trackNumber;
}
}
}
if (trackNumber == 0) {
continue;
}
if (trackNumber > Platter.head) {
Platter.head++;
moveForward = true;
} else if (trackNumber < Platter.head) {
Platter.head--;
moveForward = false;
}
}
printVals(resultSet);
}

private static void printVals(Map<Integer, Integer> resultSet) {
for (Map.Entry<Integer, Integer> iter : resultSet.entrySet()) {
System.out.println(iter.getKey() + " " + iter.getValue());
}
}

private static class Request {
int timeOfArrival;
int platterNumber;
int trackNumber;

public Request(int timeOfArrival, int platterNumber, int trackNumber) {
this.timeOfArrival = timeOfArrival;
this.platterNumber = platterNumber;
this.trackNumber = trackNumber;
}
}
}



Modifications for input reading and check on the track number can be added.

- Achilles May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this code is not working for first case.
Can you solve it.

- dude June 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

cool!!

- anuj.bhambhani May 25, 2012 | 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