JP Morgan Interview Question for Senior Software Development Engineers


Country: India
Interview Type: In-Person




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

public class ThreadPriority {
	public static void main(String[] args) throws Exception{

		//Create a countdown latch for 1 thread. (We just need to wait for 1 thread to complete)
		CountDownLatch countDown = new CountDownLatch(1);

		//Create two threads
		Thread firstThread = new Thread(new Task(countDown));
		Thread secondThread = new Thread(new Task(countDown));
		
		//Set their names
		firstThread.setName("JobOne");
		secondThread.setName("JobTwo");
		
		//Start first thread
		firstThread.start();
		//Wait for countdown to decrease
		countDown.await();
		
		//Start second thread
		secondThread.start();
		
	}
}

/**
 * The class represents a Task.
 * 
 * @author sgaur
 *
 */
class Task implements Runnable {
	CountDownLatch countLatch = null;

	public Task(CountDownLatch countDown) {
		countLatch = countDown;
	}

	@Override
	public void run() {
		for(int i = 0 ; i < 10000; i++){
			System.out.println(i + " Running " + Thread.currentThread().getName());
		}
		
		//After the process is complete, decrement the countDown
		countLatch.countDown();
	}

}

- SumitGaur April 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

working java code here
solvekarlo.com/index.php?page=15

- antipotato October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ /** * How could you make sure that thread A ,B and C run sequentially without using join method? * * @author manoj.sharma * @source Barclays */ public class ThreadJoiningWithoutJoin { public static void main(String[] args) { final JoinTask task = new JoinTask(); Thread A = new Thread(){ public void run(){ task.doJob(1, "JOB A DONE..."); } }; Thread B = new Thread(){ public void run(){ task.doJob(2, "JOB B DONE..."); } }; Thread C = new Thread(){ public void run(){ task.doJob(3, "JOB C DONE..."); } }; C.start(); B.start(); A.start(); } } // Shared Class or Object class JoinTask { private int currentRank = 1; public void doJob(int rank, String msg) { synchronized(this) { while (rank != currentRank) { try {wait();}catch(InterruptedException ex) {ex.printStackTrace();}; } System.out.println("Job:" + currentRank + " : " + msg ); currentRank++; notifyAll(); } } } }} - Manoj Sharma October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

/**
 * How could you make sure that thread A ,B and C run sequentially without using join method?
 * 
 * @author manoj.sharma
 * @source Barclays
 */

public class ThreadJoiningWithoutJoin {

	public static void main(String[] args) {
		final JoinTask task = new JoinTask();
		
		Thread A = new Thread(){
			public void run(){
				task.doJob(1, "JOB A DONE...");
			}
		};
		
		Thread B = new Thread(){
			public void run(){
				task.doJob(2, "JOB B DONE...");
			}
		};
		
		Thread C = new Thread(){
			public void run(){
				task.doJob(3, "JOB C DONE...");
			}
		};

		C.start();
		B.start();
		A.start();
		
	}

}

// Shared Class or Object 
class JoinTask {
	
	private int currentRank = 1;
	
	public void doJob(int rank, String msg) {
		synchronized(this) {
			while (rank != currentRank) {
				try {wait();}catch(InterruptedException ex) {ex.printStackTrace();};
			}
			System.out.println("Job:" + currentRank + " : " + msg );
			currentRank++;
			notifyAll();
		}
	}
}

- Manoj Sharma October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This not working

- gaurav October 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@gaurav : Please refer to solvekarlo.com/index.php?page=15 site for a good explanatory working code....

- antipotato October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code......

import java.util.concurrent.TimeUnit;

public class MyJoinTest{

	public static void main(String[] args) {
		
		MyThread t1 = new MyThread(new Runnable() {
			@Override
			public void run() {
				for(int i=0;i<5;i++){
					System.out.println("-----"+i+"-----");
					try {
						TimeUnit.SECONDS.sleep(2);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
		});
		MyThread t2 = new MyThread(new Runnable() {
			@Override
			public void run() {
				for(int i=5;i<10;i++){
					System.out.println("-----"+i+"-----");
					try {
						TimeUnit.SECONDS.sleep(2);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
		});
		MyThread t3 = new MyThread(new Runnable() {
			@Override
			public void run() {
				for(int i=10;i<15;i++){
					System.out.println("-----"+i+"-----");
					try {
						TimeUnit.SECONDS.sleep(2);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
		});
		t1.start();
		t1.myJoin();
		t2.start();
		t2.myJoin();
		t3.start();
		t3.myJoin();
		System.out.println("In Main, After Join");

	}

}

class MyThread{
	private Runnable runnable = null;
	private Thread thread = null;
	
	public MyThread(Runnable runnable) {
		this.runnable = runnable;
	}
	
	public void start(){
		thread = new Thread(runnable);
		thread.start();
	}
	public void myJoin(){
		while(thread.isAlive()){
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

- Rambrij Chauhan October 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can we use condition variables in simulating the join (in C++)?

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>

std::mutex mtx;
std::condition_variable cv;
int counter = 0;

void print_dummy(const int id) {
	std::lock_guard<std::mutex> lck(mtx);
	std::cout << "---" << id << "+++" << std::endl;
	std::this_thread::sleep_for(std::chrono::microseconds(100));
	counter++;
	cv.notify_one();
}

int main() {
	const int threads_counter = 10;
	std::vector<std::thread> tLst;
	for (int i = 0; i < threads_counter; ++i) {
		tLst.push_back(std::thread(print_dummy, i));
	}

	std::unique_lock<std::mutex> lck(mtx);
	while (counter != threads_counter) cv.wait(lck);
	
	std::cout << "Press enter to continue ..." << std::endl;
	std::cin.get();
	for (auto& t : tLst) t.detach();
	return 0;
}

- Passerby_A March 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

wait also does the same thing as join.
Once the thread completes its execution, it just notifies everyone waiting on the Thread object.

Below program worked fine and the output is:
Thread-0 Thread3.run()
Thread-1 Thread3.run()
main Thread3.run()

package threads;

public class MyJoinClass {
	
	public static void main(String[] args) {
		Thread t1 = new Thread3();
		Thread t2 = new Thread3();
		t1.start();
		myJoin(t1);
		
		t2.start();
		myJoin(t2);
		
		System.out.println(Thread.currentThread().getName() + " Thread3.run()");
		
	}

	private static void myJoin(Thread t) {
		synchronized (t) {
			if(t.getState() != Thread.State.TERMINATED)
				try {
					t.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
		}		
	}

}

class Thread3 extends Thread
{
	@Override
	public void run() {
		try {
			sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName() + " Thread3.run()");
	}
}

- Vamsi Krishna M March 18, 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