Interview Question


Country: India
Interview Type: Written Test




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

This file saved in C:\jp\co\worksap\global\ImmutableQueue.java


package jp.co.worksap.global; //package created

import java.util.ArrayList; //import arraylist
import java.util.NoSuchElementException;
import java.io.*; //import input output

public class ImmutableQueue<E> //create class ImmutableQueue, also save this file by ImmutableQueue name
{

private ArrayList<E>arr; //create private ArrayList

public ImmutableQueue() //class name costructor
{
arr=new ArrayList<E>(); //store ArrayList in arr
}

public ImmutableQueue(ArrayList<E>cl) //create ImmutableQueue function and pass arryList
{
arr=cl; //store cl data in arr
}

public ImmutableQueue<E>enqueue(E e)throws IllegalArgumentException //enqueue function, it throws exception when queue is empty
{
if(e==null) //if queue is null then throws exception
{
throw new IllegalArgumentException(); //exception name, IllegalArgumentException
}

ArrayList<E>cloneArr=new ArrayList<E>(); //create a new object of ArrayList, name as cloneArr
for(int i=0;i<arr.size();i++) //for loop used to get data from user upto arr.size(), and arr.size() used for getting size of arr
cloneArr.add(arr.get(i)); //add i th element into cloneArr
cloneArr.add(e); //add into queue
return new ImmutableQueue<E>(cloneArr); //return if operation successful/unsusseful
}

public ImmutableQueue<E>dequeue()throws NoSuchElementException //deenqueue function, it throws exception when queue is empty
{
if(arr.size()==0) //arr.size is 0 then throws exception
{
throw new NoSuchElementException(); //exception name NoSuchElementException
}
ArrayList<E>cloneArr=new ArrayList<E>(); //create a new object of ArrayList, name as cloneArr
for(int i=1;i<arr.size();i++) //for loop used to delete data from queue upto arr.size()
cloneArr.add(arr.get(i));
return new ImmutableQueue<E>(cloneArr);
}
/*
public E peek()throws NoSuchElementException
{
if(arr.size()==0)
{
throw new NoSuchElementException(); //queue empty then throws exception
}
else
{
return arr.get(0); //this is used for displyaing only next element of queue
}
}
*/
public void peekAll()throws NoSuchElementException //peekAll function used for displaying all next element
{
if(arr.size()==0) //if queue empty then throws exception
{
throw new NoSuchElementException(); //name of exception, NoSuchElementException
}
else //otherwise
{
int queueSize = arr.size(); //arr.size() means arr size stored in queueSize variable, declare as integer
for(int i=0;i<queueSize;i++) //loop work from 0 to queueSize
{
System.out.println(arr.get(i)); //print arr
}
}
}
public int size() //size function used for calcutaing size, and it can be used in all prog
{
return arr.size(); //returns the size of arr
}
}




This file saved in C:\Queue.java


import jp.co.worksap.global.*; //imported that created package in jp\co\worksap\global\
import java.io.*; //input output package

public class Queue //class name is queue, so file name is queue
{
public static void main(String args[]) throws IOException //its a main method and throws input and output exception if any error occeured
{
ImmutableQueue <Integer> q=new ImmutableQueue <Integer>(); //create a object name as q, of a class Immutable queue, in a another file name as ImmutableQueue
BufferedReader bin=new BufferedReader(new InputStreamReader(System.in)); //it can be used for reading input string from user
System.out.println("How Many Elements: "); //normal msg for printing data on screen
int ele=Integer.parseInt(bin.readLine()); //conversion of string into number and store in ele variable (ele get)
System.out.println("\nEnter Elements: "); //normal msg for printing data on screen
for(int i=0;i<ele;i++) //for loop starts from 0 to ele, and ele already get from user
{
q=q.enqueue(Integer.parseInt(bin.readLine())); //enqueu operation, means insert element into queue
}
q=q.dequeue(); //deleted first element of queue
/* System.out.println("\nnext Queue Element is: ");

System.out.println(q.peek()); //peek used for displyaing next element of queue. coz fist element is deleted by deuqueue
*/
System.out.println("\nNew Queue Elements: ");
q.peekAll(); //peekAll used for displaying all next elemeny]t of queue, coz first element is deleted bt dequeue

}
}

- Nitin Vitthal Sonawane August 27, 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