JP Morgan Interview Question
Java DevelopersCountry: India
Interview Type: Written Test
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;
}
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;
}
}
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();
}
}
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();
}
}
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);
}
}
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);
}
}
- mger1979 May 16, 2017