Oracle Interview Question for Developer Program Engineers






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

oh boy! lotta-code!

1. Implement List<E>, Serializable, Cloneable, RandomAccess
2. Make sure that iterators are fail-fast (can be done using a single modification counter)
3. Finally make sure that sequential access with myList[i] is faster than myList.listIterator().next

- rajanikant.malviya August 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you please provide the skeleton of the implementation class..?

- guest123 August 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code for your own ArrayList program.

import java.util.RandomAccess;

public class MyOwnArrayList implements RandomAccess, Cloneable, java.io.Serializable {

    private transient Object[] elementData;  //Making one array to hold data
    private int size;
    protected transient int modCount = 0;  //Counter to find how many times Array is re-sized.
    private static final long serialVersionUID = 1234L;

    public MyOwnArrayList() {  //Making an initial Array of size 10.
        this(10);
    }
    public MyOwnArrayList(int initialCapacity) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
        this.elementData = (Object[])new Object[initialCapacity];
    }

    public boolean add(Object obj) {
        validateCapacity(size + 1);  
        elementData[size++] = obj;
        return true;
    }

    public Object get(int index) {
        Object obj=elementData[index];
        return obj;
    }

    public int size() {
        return size;
    }
    public void validateCapacity(int minCapacity) {
        modCount++;
        int oldCapacity = elementData.length;
        if (minCapacity > oldCapacity) {
            Object oldData[] = elementData;
            int newCapacity = (oldCapacity * 3)/2 + 1; //Size increases by 1.5 times+1.

            if (newCapacity < minCapacity)
                newCapacity = minCapacity;
            elementData = (Object[])new Object[newCapacity];            
            System.arraycopy(oldData, 0, elementData, 0, size); //src, srcPos, dest, destPos, length
            //System.arraycopy(src, srcPos, dest, destPos, length);
        }
    }

    public Object remove(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);

        modCount++;
        Object oldValue = elementData[index];

        int numMoved = size - index - 1;
        if (numMoved > 0) {
            //System.arraycopy(src, srcPos, dest, destPos, length);
            System.arraycopy(elementData, index+1, elementData, index, numMoved);
        }
        elementData[--size] = null; // Let gc do its work
        return oldValue;
    }
    
    //Defining iterator method
    public FruitIterator iterator() {
        System.out.println("My overridded iterator method called in Fruit class");
        return new FruitIterator(this);
    }
}

************Mine designed Iterator***********
public class FruitIterator {
    private MyOwnArrayList fruitList;
    private int position;

    public FruitIterator(MyOwnArrayList fruitList) {
        this.fruitList = fruitList;
    }

    public boolean hasNext() {
        if (position < fruitList.size())
            return true;
        else
            return false;
    }

    public Object next() {
        Object aniObj = fruitList.get(position);
        position++;
        return aniObj;
    }
    public void remove() {
        fruitList.remove(position);
    }
}


//Program: MyFruitIteratorExample.java, Execute the below program.
public class MyFruitIteratorExample {
    public static void main(String[] args) {
        
        MyOwnArrayList fruitList = new MyOwnArrayList();
        fruitList.add("Mango");
        fruitList.add("Strawaberry");
        fruitList.add("Papaya");
        fruitList.add("Watermalon");
        
        System.out.println("-----Calling my iterator on my ArrayList-----");
        FruitIterator it=fruitList.iterator();        
        while(it.hasNext()){
            String s=(String)it.next();
            System.out.println("Value: "+s);
        }
        System.out.println("--Fruit List size: "+fruitList.size());
        fruitList.remove(1);
        System.out.println("--After removal, Fruit List size: "+fruitList.size());
    }
}

- oglA December 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think I don't need to explain. I have explained in comments.

package interview;

public class MyArrayList {

    private static int initialCapacity = 5;
    private static int currentSize;
    private static Object[] myObject, temp;
    private static int currentIndex = 0;

    public MyArrayList() {  //creates default sized Array of Objects
        myObject = new Object[initialCapacity];
        /* evrytime I cross my capacity, 
    I make double size of Object Array, copy all the elements from past myObject Array Object
         */
    }

    public MyArrayList(int size) { //creates custom sized Array of Objects
        myObject = new Object[size];
    }

    void add(Object string) {
        //add element directy
        myObject[currentIndex] = string;
        currentSize = myObject.length;
        currentIndex++;
        if (currentIndex == currentSize) {
            createDoubleSizedObjectArray(currentSize);

        }

    }

    private void createDoubleSizedObjectArray(int currentSize) {
        temp = myObject.clone();
        myObject = new Object[2 * currentSize];  //myObject pointer big size data structure

//         myObject = temp.clone(); //probably I can do this here as well. Need to check this
        System.arraycopy(temp, 0, myObject, 0, currentSize);

    }

    void delete(String string) {
        //if already empty 
        if (currentIndex == 0) {
            System.out.println("Already empty!");
        }
        //you don't need to delete anything
        currentIndex--;

    }

}


package interview;

public class arrayListImpl {
    public static void main(String[] args) {
        MyArrayList mArrayList = new MyArrayList();
        mArrayList.add("123");
        mArrayList.delete("123");
   
    }
}

- Uddhav Gautam July 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think I don't need to explain. I have explained in comments.


package interview;

public class MyArrayList {

private static int initialCapacity = 5;
private static int currentSize;
private static Object[] myObject, temp;
private static int currentIndex = 0;

public MyArrayList() { //creates default sized Array of Objects
myObject = new Object[initialCapacity];
/* evrytime I cross my capacity,
I make double size of Object Array, copy all the elements from past myObject Array Object
*/
}

public MyArrayList(int size) { //creates custom sized Array of Objects
myObject = new Object[size];
}

void add(Object string) {
//add element directy
myObject[currentIndex] = string;
currentSize = myObject.length;
currentIndex++;
if (currentIndex == currentSize) {
createDoubleSizedObjectArray(currentSize);

}

}

private void createDoubleSizedObjectArray(int currentSize) {
temp = myObject.clone();
myObject = new Object[2 * currentSize]; //myObject pointer big size data structure

// myObject = temp.clone(); //probably I can do this here as well. Need to check this
System.arraycopy(temp, 0, myObject, 0, currentSize);

}

void delete(String string) {
//if already empty
if (currentIndex == 0) {
System.out.println("Already empty!");
}
//you don't need to delete anything
currentIndex--;

}

}


package interview;

public class arrayListImpl {
public static void main(String[] args) {
MyArrayList mArrayList = new MyArrayList();
mArrayList.add("123");
mArrayList.delete("123");

}
}

- Uddhav Gautam July 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package interview;

public class MyArrayList {

    private static int initialCapacity = 5;
    private static int currentSize;
    private static Object[] myObject, temp;
    private static int currentIndex = 0;

    public MyArrayList() {  //creates default sized Array of Objects
        myObject = new Object[initialCapacity];
        /* evrytime I cross my capacity, 
    I make double size of Object Array, copy all the elements from past myObject Array Object
         */
    }

    public MyArrayList(int size) { //creates custom sized Array of Objects
        myObject = new Object[size];
    }

    void add(Object string) {
        //add element directy
        myObject[currentIndex] = string;
        currentSize = myObject.length;
        currentIndex++;
        if (currentIndex == currentSize) {
            createDoubleSizedObjectArray(currentSize);

        }

    }

    private void createDoubleSizedObjectArray(int currentSize) {
        temp = myObject.clone();
        myObject = new Object[2 * currentSize];  //myObject pointer big size data structure

//         myObject = temp.clone(); //probably I can do this here as well. Need to check this
        System.arraycopy(temp, 0, myObject, 0, currentSize);

    }

    void delete(String string) {
        //if already empty 
        if (currentIndex == 0) {
            System.out.println("Already empty!");
        }
        //you don't need to delete anything
        currentIndex--;

    }

}


package interview;

public class arrayListImpl {
    public static void main(String[] args) {
        MyArrayList mArrayList = new MyArrayList();
        mArrayList.add("123");
        mArrayList.delete("123");
   
    }
}

- Uddhav Gautam July 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.Serializable;
import java.util.Iterator;
import java.util.RandomAccess;
import java.util.function.Consumer;

public class MyCustomArrayList<E> implements RandomAccess, Cloneable, java.io.Serializable{

private Object[] elementData;
private int size;

public MyCustomArrayList() {
this(10);
}

public MyCustomArrayList(int initialCapacity) {
if(initialCapacity < 0)
new IllegalArgumentException("Illeagl Length of the Array"+initialCapacity);
elementData = new Object[initialCapacity];
}

public boolean add(Object element){
ensureCapacity(size+1);
elementData[size++] = element;
return true;
}

public void ensureCapacity(int positionalLength){
int actualArrayLength = elementData.length;
Object[] arrayCopyObject;
int newArrayLength;
if(positionalLength > actualArrayLength){
arrayCopyObject= elementData;
newArrayLength = (actualArrayLength * 3 )/2 + 1;
if(newArrayLength < positionalLength){
newArrayLength = positionalLength;
}
elementData = new Object[newArrayLength];
System.arraycopy(arrayCopyObject, 0, elementData, 0, newArrayLength);
}
}

public Object get(int index){
Object obj = elementData[index];
return obj;
}

public int size(){
return size;
}

public CustomIterator iterator(){
return new CustomIterator(this);
}

private class CustomIterator<E> implements Iterator<E>{

private MyCustomArrayList customList;
private int position;

public CustomIterator(MyCustomArrayList customList) {
this.customList = customList;
}

public boolean hasNext() {
if(position < customList.size())
return true;
else
return false;
}

public E next() {
Object aniObj = customList.get(position);
position++;
return (E) aniObj;
}

public void remove() {

}
}

/**
* @param args
*/
public static void main(String[] args) {
MyCustomArrayList<Object> list = new MyCustomArrayList<Object>();
list.add("Apple");
list.add("Banana");
list.add("Canberry");
list.add("Mango");

System.out.println("-----Calling my iterator on my ArrayList-----");

CustomIterator<Object> itr = list.iterator();
while(itr.hasNext()){
Object obj=(Object)itr.next();
System.out.println("Value: "+obj.toString());
}
System.out.println("--Fruit List size: "+list.size());
//fruitList.remove(1);
System.out.println("--After removal, Fruit List size: "+list.size());

}
}

The Above first Program works Fine but If I have included the Generics then its is throwing the compile time exception at custom Iterator saying that static reference call to non static reference is not allowed. Please help me on this.

- Rani December 06, 2017 | 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