Barclays Capital Interview Question
Solutions ArchitectsCountry: United States
I started in this way-
1.Identified the entity. -
Here as per my understanding three entities are there. 1. Track, 2. Paticipants 3. Events.
So In Java I will write Three Interfaces with abstruct behaviour.
2. Identify the relation.
Event 'has-a' 'participants' and 'track' and it is composition.
3.To start the event -
Say when line man wil say ready, set and Fire. - This could be implemented using CountDownLatch with an initial value. When the event will start, the CountDownLatch latch will be zero. and I have to create 10 threads (i.e 10 participants). Each thread is a participants.
4. To create the participant thread , I will create a thread pool of size 10. The task for each thread is to count down counter which is nothing but the event type. If it is 100 m then each thread in execution will start count down from 100 to 0.
5. We need to store the time of each thread execution. So I said I will take a Map.
whose key will be the participant and value will be the time.
6.To find the winner, I have to short the map based on value. So I need to use Comparator.
Please let me know your thoughts...
public class Event { String eventType; getter & setter}
public class Participant { long startTime; long endTime; getter setter}
public class Race {String eventType; Participant [] participant }
I have a very rough solution for this but I think its good enough to explain quickly.
We can have Event listener when a player completes the race. The player name and current time is saved as Key,Value in LinkedHashMap and the player with minimum time value(Long) is the winner for any race. Yes, we can design it more beautifully but this was just my starting thought.
public class Event {
private String name;
private int distance;
private List<Athlete> athelets;
public Event(String name, int distance, List<Athlete> athelets) {
this.name = name;
this.distance = distance;
this.athelets = athelets;
}
public void startRace() {
ExecutorService executor = Executors.newFixedThreadPool(10);
CompletionService<String> completionService = new ExecutorCompletionService<String>(
executor);
for (Athlete athelet : athelets) {
completionService.submit(athelet);
}
executor.shutdown();
for (int rank = 0; rank < athelets.size(); rank++) {
Future<String> completedAthele;
try {
completedAthele = completionService.take();
System.out.format("Rank :: %d Name %s \n", (rank + 1),
completedAthele.get());
} catch (InterruptedException e1) {
} catch (ExecutionException e) {
}
}
}
public void displayResults() {
}
}
public class Athlete implements Callable<String>{
Athlete(String name){
this.name = name;
}
private static Random randomInt = new Random();
public String name;
@Override
public String call() {
try {
int time = randomInt.nextInt(10) ;
System.out.println(time);
Thread.currentThread().sleep( time * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return name;
}
}
public class Organizer {
public static void main(String args[]){
List<Athlete> athletes = new ArrayList<Athlete>();
athletes.add( new Athlete("Durga"));
athletes.add( new Athlete("Lavanya"));
athletes.add( new Athlete("Pinky"));
Event event = new Event("100 Meter Run", 100, athletes);
event.startRace();
}
}
Hi Guys can you please review my code and feel free to suggest me any improvement. Thanks, In advance.
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Design the below scenario in Java. In an Olympic event there is a running
* track and it is used for 100m,200m,400m. You have 10 participants. When the
* event start , capture the time taken by each participants. Determine who is
* the winner in each event.
*
* @author mis
*
*/
public class RacingProblem {
public static <T> void main(String args[]) {
Map<Integer, Participant> participlantDetails = new HashMap<>();
participlantDetails.put(1, new Participant(new Date(), getDateWithAdditionOfGivenMinute(new Date(), 4),
getDateWithAdditionOfGivenMinute(new Date(), 15), getDateWithAdditionOfGivenMinute(new Date(), 20)));
participlantDetails.put(2, new Participant(new Date(), getDateWithAdditionOfGivenMinute(new Date(), 5),
getDateWithAdditionOfGivenMinute(new Date(), 5), getDateWithAdditionOfGivenMinute(new Date(), 25)));
participlantDetails.put(3, new Participant(new Date(), getDateWithAdditionOfGivenMinute(new Date(), 6),
getDateWithAdditionOfGivenMinute(new Date(), 20), getDateWithAdditionOfGivenMinute(new Date(), 15)));
participlantDetails.put(4, new Participant(new Date(), getDateWithAdditionOfGivenMinute(new Date(), 7),
getDateWithAdditionOfGivenMinute(new Date(), 6), getDateWithAdditionOfGivenMinute(new Date(), 16)));
participlantDetails.put(5, new Participant(new Date(), getDateWithAdditionOfGivenMinute(new Date(), 8),
getDateWithAdditionOfGivenMinute(new Date(), 7), getDateWithAdditionOfGivenMinute(new Date(), 17)));
participlantDetails.put(6, new Participant(new Date(), getDateWithAdditionOfGivenMinute(new Date(), 9),
getDateWithAdditionOfGivenMinute(new Date(), 8), getDateWithAdditionOfGivenMinute(new Date(), 18)));
participlantDetails.put(7, new Participant(new Date(), getDateWithAdditionOfGivenMinute(new Date(), 10),
getDateWithAdditionOfGivenMinute(new Date(), 9), getDateWithAdditionOfGivenMinute(new Date(), 19)));
participlantDetails.put(8, new Participant(new Date(), getDateWithAdditionOfGivenMinute(new Date(), 11),
getDateWithAdditionOfGivenMinute(new Date(), 10), getDateWithAdditionOfGivenMinute(new Date(), 21)));
participlantDetails.put(9, new Participant(new Date(), getDateWithAdditionOfGivenMinute(new Date(), 12),
getDateWithAdditionOfGivenMinute(new Date(), 11), getDateWithAdditionOfGivenMinute(new Date(), 22)));
participlantDetails.put(10, new Participant(new Date(), getDateWithAdditionOfGivenMinute(new Date(), 1),
getDateWithAdditionOfGivenMinute(new Date(), 12), getDateWithAdditionOfGivenMinute(new Date(), 23)));
List<Event> eventDetails = new ArrayList<>();
long timeTakenFor100M = 0;
long timeTakenFor200M = 0;
long timeTakenFor400M = 0;
Participant p = null;
for (Map.Entry<Integer, Participant> entry : participlantDetails.entrySet()) {
p = entry.getValue();
timeTakenFor100M = getTimeToCompleteEvent(p.eventStartDateTime, p.eventEndDateTimeAt100M);
timeTakenFor200M = getTimeToCompleteEvent(p.eventStartDateTime, p.eventEndDateTimeAt200M);
timeTakenFor400M = getTimeToCompleteEvent(p.eventStartDateTime, p.eventEndDateTimeAt400M);
eventDetails.add(new Event(entry.getKey(), p, timeTakenFor100M, timeTakenFor200M, timeTakenFor400M));
}
Collections.sort(eventDetails, new Comparator<Event>() {
@Override
public int compare(Event o1, Event o2) {
return ((Long)o1.timeTakenFor100M).compareTo((Long)o2.timeTakenFor100M);
}
});
System.out.println(eventDetails.get(0).pId);
Collections.sort(eventDetails, new Comparator<Event>() {
@Override
public int compare(Event o1, Event o2) {
return ((Long)o1.timeTakenFor200M).compareTo((Long)o2.timeTakenFor200M);
}
});
System.out.println(eventDetails.get(0).pId);
Collections.sort(eventDetails, new Comparator<Event>() {
@Override
public int compare(Event o1, Event o2) {
return ((Long)o1.timeTakenFor400M).compareTo((Long)o2.timeTakenFor400M);
}
});
System.out.println(eventDetails.get(0).pId);
}
private static long getTimeToCompleteEvent(Date eventStartDateTime, Date eventEndDateTime) {
return eventEndDateTime.getTime() - eventStartDateTime.getTime();
}
private static Date getDateWithAdditionOfGivenMinute(Date date, int minute) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, minute);
return cal.getTime();
}
static class Event {
int pId;
Participant p;
long timeTakenFor100M;
long timeTakenFor200M;
long timeTakenFor400M;
public Event(int pId, Participant p, long timeTakenFor100M, long timeTakenFor200M, long timeTakenFor400M) {
this.pId = pId;
this.p = p;
this.timeTakenFor100M = timeTakenFor100M;
this.timeTakenFor200M = timeTakenFor200M;
this.timeTakenFor400M = timeTakenFor400M;
}
}
static class Participant {
Date eventStartDateTime;
Date eventEndDateTimeAt100M;
Date eventEndDateTimeAt200M;
Date eventEndDateTimeAt400M;
public Participant(Date eventStartDateTime, Date eventEndDateTimeAt100M, Date eventEndDateTimeAt200M,
Date eventEndDateTimeAt400M) {
this.eventStartDateTime = eventStartDateTime;
this.eventEndDateTimeAt100M = eventEndDateTimeAt100M;
this.eventEndDateTimeAt200M = eventEndDateTimeAt200M;
this.eventEndDateTimeAt400M = eventEndDateTimeAt400M;
}
}
}
Can you clarify the question? What is the input, array of time intervals of size 10?
- oOZz May 31, 2013