JP Morgan Interview Question for Java Developers


Country: India
Interview Type: Written Test




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

public static class QueueBlocking<T> {
        
        LinkedList<T> queue = new LinkedList<T>();

        void push(T t) {
            synchronized (queue) {
                queue.push(t);
            }
        }

        T peek() {
            return queue.peek();
        }

        T poll() {
            T t = null;
            synchronized (queue) {
                t = queue.poll();
            }
            while (t == null) {
                synchronized (queue) {
                    t = queue.poll();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return t;
        }
    }

- mger1979 May 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public List<String> getPermutatuion(String s) {
		List<String> list = new ArrayList<String>();
		List<String> tempList = new ArrayList<String>();
		list.add(new String(s));
		tempList.add(new String(s));
		for(int i=s.length()-1; i>=0; i--) {
			for(String str : tempList) {
				char[] temp = Arrays.copyOf(str.toCharArray(), str.length());
				temp[i] = (char)(temp[i]&'_');
				list.add(new String(temp));
			}
			tempList.clear();
			tempList.addAll(list);
		}
		return list;

}

- pankaj May 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.java.algorithm;

public class BlockingQueue<E> {

	private Object[] items = new Object[100];

	private int putptr;

	private int takeptr;

	private int count;

	public synchronized void put(E element) throws InterruptedException {
		while (items.length == count) {
			wait();
		}
		items[putptr] = element;
		if (++putptr == items.length) {
			putptr = 0;
		}
		count++;
		if (count == 0) {
			notifyAll();
		}
	}

	@SuppressWarnings("unchecked")
	public synchronized E take() throws InterruptedException {
		while (count == 0) {
			wait();
		}
		E element = (E) items[takeptr];
		items[takeptr] = null;
		if (++takeptr == items.length) {
			takeptr = 0;
		}
		count--;
		if (items.length == count) {
			notifyAll();
		}
		return element;

	}

}

- VIKASH July 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.LinkedList;
import java.util.Queue;

public class CustomBlockingQueueImpl {


	
	public static void main (String[] args) throws InterruptedException{
		final CustomBlockingQueue<Integer> queue = new CustomBlockingQueue<Integer>(
				new LinkedList<Integer>(), 10);
		Runnable target1 = new Runnable() {

			@Override
			public void run() {
				for (int i = 1; i <= 20; i++) {
					try {
						queue.put(i);
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		};

		Runnable target2 = new Runnable() {

			@Override
			public void run() {
				for (int i = 1; i <= 20; i++) {
					try {
						Integer take = queue.take();
						System.out.println("queue.take() : " + take);
						Thread.sleep(500);
						Integer peek = queue.peek();
						System.out.println("queue.peek() : " + peek);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		};
		
		Thread t1 = new Thread(target1,"T1");
		
		t1.start();
		
		Thread.sleep(1000);
		
	Thread t2 = new Thread(target2,"T2");
		
		t2.start();
	}
}

class CustomBlockingQueue<E> {

	private Queue<E> queue = null;
	private volatile int capacity;

	CustomBlockingQueue(Queue<E> queue, int capacity) {
		this.queue = queue;
		this.capacity = capacity;
	}

	public void put(E e) throws InterruptedException {
		synchronized (queue) {

			while (queue.size() == capacity) {
				queue.wait();
			}
			System.out.println("Putting : " + e);
			queue.add(e);
			queue.notify();
		}
	}

	public E take() throws InterruptedException {
		E e = null;
		synchronized (queue) {
			while (queue.isEmpty())
				queue.wait();

			e = queue.remove();
			System.out.println("Removing "+e);
			queue.notify();
		}

		return e;
	}

	public E peek() {
		return queue.peek();
	}
}

- sam December 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.LinkedList;
import java.util.Queue;

public class CustomBlockingQueueImpl {


	
	public static void main (String[] args) throws InterruptedException{
		final CustomBlockingQueue<Integer> queue = new CustomBlockingQueue<Integer>(
				new LinkedList<Integer>(), 10);
		Runnable target1 = new Runnable() {

			@Override
			public void run() {
				for (int i = 1; i <= 20; i++) {
					try {
						queue.put(i);
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		};

		Runnable target2 = new Runnable() {

			@Override
			public void run() {
				for (int i = 1; i <= 20; i++) {
					try {
						Integer take = queue.take();
						System.out.println("queue.take() : " + take);
						Thread.sleep(500);
						Integer peek = queue.peek();
						System.out.println("queue.peek() : " + peek);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		};
		
		Thread t1 = new Thread(target1,"T1");
		
		t1.start();
		
		Thread.sleep(1000);
		
	Thread t2 = new Thread(target2,"T2");
		
		t2.start();
	}
}

class CustomBlockingQueue<E> {

	private Queue<E> queue = null;
	private volatile int capacity;

	CustomBlockingQueue(Queue<E> queue, int capacity) {
		this.queue = queue;
		this.capacity = capacity;
	}

	public void put(E e) throws InterruptedException {
		synchronized (queue) {

			while (queue.size() == capacity) {
				queue.wait();
			}
			System.out.println("Putting : " + e);
			queue.add(e);
			queue.notify();
		}
	}

	public E take() throws InterruptedException {
		E e = null;
		synchronized (queue) {
			while (queue.isEmpty())
				queue.wait();

			e = queue.remove();
			System.out.println("Removing "+e);
			queue.notify();
		}

		return e;
	}

	public E peek() {
		return queue.peek();
	}
}

- sam December 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BlockingQueue {

  private List queue = new LinkedList();
  private int  limit = 10;

  public BlockingQueue(int limit){
    this.limit = limit;
  }


  public synchronized void enqueue(Object item)
  throws InterruptedException  {
    while(this.queue.size() == this.limit) {
      wait();
    }
    if(this.queue.size() == 0) {
      notifyAll();
    }
    this.queue.add(item);
  }


  public synchronized Object dequeue()
  throws InterruptedException{
    while(this.queue.size() == 0){
      wait();
    }
    if(this.queue.size() == this.limit){
      notifyAll();
    }

    return this.queue.remove(0);
  }

}

- sarojinigarapati May 24, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BlockingQueue {

  private List queue = new LinkedList();
  private int  limit = 10;

  public BlockingQueue(int limit){
    this.limit = limit;
  }


  public synchronized void enqueue(Object item)
  throws InterruptedException  {
    while(this.queue.size() == this.limit) {
      wait();
    }
    if(this.queue.size() == 0) {
      notifyAll();
    }
    this.queue.add(item);
  }


  public synchronized Object dequeue()
  throws InterruptedException{
    while(this.queue.size() == 0){
      wait();
    }
    if(this.queue.size() == this.limit){
      notifyAll();
    }

    return this.queue.remove(0);
  }

}

- sarojinigarapati May 24, 2018 | 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