Interview Question for Software Engineers


Country: United States




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

We can maintain a queue of tasks inside the MyTimer class, and do the following whenever mySchedule(runnable,int ) is called:
1.if queue is empty, push the new task in the queue, and call system.schedule() on task at head of the queue.
2.if queue is not empty push the task.
3.When the call to system.schedule() completes, dequeue task from taskqueue and call schedule on it.

- Kumar April 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Task {
	private Runnable runnable;
	private int seconds;

	public Runnable getRunnable() {
		return runnable;
	}

	public void setRunnable(Runnable runnable) {
		this.runnable = runnable;
	}
	public int getSeconds() {
		return seconds;
	}

	public void setSeconds(int seconds) {
		this.seconds = seconds;
	}
}

public class MyTimer extends System{
	Queue<Task> queue = new LinkedList<Task>();
	Boolean flag; 
	
	@Override
	public void schedule(Runnable runnable, int seconds) {
		if(!queue.isEmpty()){
			Task task = queue.remove();
			if(flag){
				super.schedule(task.getRunnable(), task.getSeconds());
			}
			
			flag = true;
			
		} else{
				super.schedule(runnable, seconds);	
			}
		
		Task newTask = new Task();
		newTask.setRunnable(runnable);
		newTask.setSeconds(seconds);
		queue.add(newTask);
	}

}

- saddham April 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A more simple solution is given below:

public class Task {
	private Runnable runnable;
	private int seconds;

	public Runnable getRunnable() {
		return runnable;
	}

	public void setRunnable(Runnable runnable) {
		this.runnable = runnable;
	}

	public int getSeconds() {
		return seconds;
	}

	public void setSeconds(int seconds) {
		this.seconds = seconds;
	}
}

public class MyTimer extends System{
	Queue<Task> queue = new LinkedList<Task>();

	@Override
	public void schedule(Runnable runnable, int seconds) {
		Task newTask = new Task();
		newTask.setRunnable(runnable);
		newTask.setSeconds(seconds);
		queue.add(newTask);
		
		Task task = queue.remove();
		super.schedule(task.getRunnable(), task.getSeconds());
	}	

}

- saddham April 18, 2015 | 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