JP Morgan Interview Question for Java Developers


Country: United States
Interview Type: Phone Interview




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

Add the tasks to ordered list and iterate over the list to get the task and execute

- vsalaksh August 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hmmm, my answer was on those lines, but he said he wanted me to use a class from Java concurrent package...

- User1 August 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution using classes from ConcurrentPackage:

Algorithm:

1. Create a Semaphore and initialize with N -the number of tasks.
2. Create a task X which needs Y permits to execute. ( values of Y -> 1 to N)
3. The value of Y to assign for a task depends on after how many tasks it should start execute.
3. once the task X completes, it release the Y permits.
4. For example, the task which needs to be executed at first assigned value 1 to Y, similarly the second task assigned with value 2 for Y, like that the last task assigned N value for Y
5. Start all the tasks.

This Semaphore will ensure all the tasks run sequentially

- vsalaksh September 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;


public class SequentialTaskExecutor {
	
	ExecutorService service = Executors.newFixedThreadPool(10);
	Semaphore sem = new Semaphore(10);
	
	void runTasks()
	{
		try
		{
			sem.acquire(9);
			service.submit(new CustomTask(2, sem));
			service.submit(new CustomTask(4, sem));
			service.submit(new CustomTask(3, sem));
			service.submit(new CustomTask(1, sem));
			service.submit(new CustomTask(7, sem));
			service.submit(new CustomTask(6, sem));
			service.submit(new CustomTask(8, sem));
			service.submit(new CustomTask(9, sem));
			service.submit(new CustomTask(10, sem));
			service.submit(new CustomTask(5, sem));
		}
		catch (InterruptedException exc)
		{
			exc.printStackTrace();
		}
		
	}
	
	public static void main(String[] args)
	{
		SequentialTaskExecutor executor = new SequentialTaskExecutor();
		executor.runTasks();
	}

}

class CustomTask implements Runnable
{
	private int seqPosition;
	private Semaphore sem;
	
	CustomTask(int seqPosition, Semaphore sem)
	{
		this.seqPosition = seqPosition;
		this.sem = sem;
		System.out.println("Created Task : " + seqPosition);
	}
	
	public void run()
	{
		try
		{
			for (;;)
			{
				boolean isAcquired = sem.tryAcquire(seqPosition, 1000, TimeUnit.MILLISECONDS);
				if (!isAcquired)
				{
					Thread.sleep(2000);
					continue;
				}
				else
					break;
			}
			
			System.out.println("Task " + seqPosition + " is completed");
			sem.release(seqPosition+1);
		}
		catch (InterruptedException exc)
		{
			//do nothing
		}
	}
}

- vsalaksh September 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your approach is good. But you have used only for 10 task. For 100 task we have to submit 100 task and may be used with 10 or more threads. Right?

- tushargoel86 September 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution has 1 problem. Lets say if 5th thread is finished, then any thread from 1 to 6 can acquire semaphore.

- Akash Mahajan May 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package practise;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Created by rajeswav on 6/18/2016.
*/
public class task {
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newSingleThreadExecutor();

for(int i=0;i<100;i++){
executor.submit(new Runnable() {
public void run() {
System.out.print("call task methods");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
executor.shutdown();
executor.awaitTermination(2, TimeUnit.DAYS);
}
}

- Raji June 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess interviewer meant executorService.

package practise;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Created by rajeswav on 6/18/2016.
*/
public class task {
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newSingleThreadExecutor();

for (int i = 0; i < 100; i++) {
executor.submit(new Runnable() {
public void run() {
System.out.print("call task methods");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
executor.shutdown();
executor.awaitTermination(2, TimeUnit.DAYS);
}
}
}

- Raji June 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package practise;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Created by rajeswav on 6/18/2016.
*/
public class task {
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(1);

for (int i = 0; i < 100; i++)
executor.submit(new Runnable() {
public void run() {
System.out.print("call task methods");
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
System.out.println("All threads are submitted");
executor.shutdown();
executor.awaitTermination(2, TimeUnit.DAYS);
}
}

- Raji June 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using SingleThreadExecutor we can assign 1000 tasks sequentially without worrying about Synchronization.

- Anonymous July 10, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Since it says the 100 tasks are to be done sequentially, I don't see the need to use threads at all. Simply sequentially executing the tasks from the same thread would do.

- teli.vaibhav August 25, 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