In this post I am sharing coding interview questions asked in java interviews.
Question 1:
Write the code for custom BlockingQueue in java.
Answer:
There are two custom implementations of BlockingQueue: Using Synchronization and using Lock and Condition objects.
Using synchronization:
public class CustomBlockingQueue{
int putPtr, takePtr;
Object[] items;
int count = 0;
public CustomBlockingQueue(int length){
items = new Object[length];
}
public void synchronized put(Object item){
while(count == items.length()){
try{
wait();
}
catch(InterruptedException e){
}
items[putPtr] = item;
if(++putPtr == items.length)
putPtr = 0;
count++;
notifyAll();
}
}
public void synchronized take(){
while(count == 0){
try{
wait();
}
catch(InterruptedException e){
}
}
Object item = items[takePtr];
if(++takePtr == items.length)
takePtr = 0;
--count;
notifyAll();
}
}
Using Lock and Condition object:
public class CustomBlockingQueue{
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
public Object[] items = new Object[100];
int putPtr, takePtr, count;
public void put(Object item) throws InterruptedException{
lock.lock();
try{
while(count == items.length)
notFull.await();
items[putPtr] = item;
if(++putPtr = items.length)
putPtr = 0;
++count;
nonEmpty.signal();
}
finally{
lock.unlock();
}
}
public Object take() throws InterruptedException{
lock.lock();
try{
while(count == 0)
notEmpty.await();
Object x = items[takePtr];
if(++takePtr == items.length)
takePtr = 0;
count--;
notFull.signal();
return x;
}
finally{
lock.unlock();
}
}
}
Question 2:
Write the code for custom ArrayList in java.
Answer:
public class CustomArrayList{
public Object[] items;
public int ptr;
public CustomArrayList(){
items = new Object[100];
}
public void add(Object item){
if(items.length - ptr == 5){
increaseListSize();
}
items[ptr++] = item;
}
public Object get(int index){
if(index < ptr)
return items[index];
else{
throw new ArrayIndexOutOfBoundsException();
}
}
private void increaseListSize(){
items = Arrays.copyOf(items, items.length*2);
}
}
Question 3:
Write the code for implementing own stack.
Answer:
public class CustomStack{
private in maxSize;
private long[] stackArray;
private int top;
public CustomStack(int size){
maxSize = s;
stackArray = new long[size];
top= -1;
}
public void push(long l){
stackArray[++top] = l;
}
public long pop(){
return stackArray[top--];
}
public long peek(){
return stackArray[top];
}
public boolean isEmpty(){
return (top == -1);
}
}
Question 4:
Write the code for implementing own Queue.
Answer:
public class CustomQueue{
private int[] arrQueue;
private int front , rear;
public CustomQueue(int size){
arrQueue[size] = new int[];
front = rear = -1;
}
public void insert(int item){
if(rear == -1){
front = rear = 0;
}
else{
rear++;
}
if(arrQueue.length == rear)
increaseQueueSize();
arrQueue[rear] = item;
}
public int remove(){
if(arrQueue.length == 0)
throw new NoSuchElementException();
int elem = arrQueue[front];
if(front == rear){
front = rear = -1;
}
else
front++;
return elem;
}
public void increaseQueueSize(){
arrQueue = Arrays.copyOf(arrQueue, arrQueue.length*2);
}
}
That's all for this post.
Thanks for reading!!
No comments:
Post a Comment