Oracle Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

Make the new and new[] private.
Have a static member variable for the class and everytime the constructor is hit, increment the variable. Also add a check in the constructor to check if the static variable is equal to 5, if yes, throw a bad_alloc

- Sunil Raj February 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can have a static counter set to 5 and decrement it for each object created.

- Kstar February 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

When a object is created as a local variable its memory will be allocated on stack. So if we create the object without using dynamic memory allocation it will be on stack.

To limit the 'No of objects' created use a static method in class which will return the corresponding object, with static variable count being incremented for each object creation and checked for limit 5.

- Srinivasan February 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//By pop each one to another stack (counting) and push each one backpublic class SizedStack {
	public static void main(String[] args) {
		Stack test = new Stack(5);
		try {
			test.push(new NodeX("a"));
			test.push(new NodeX("b"));
			test.push(new NodeX("c"));
			test.push(new NodeX("d"));
			test.push(new NodeX("e"));
			test.push(new NodeX("f"));
		} catch (StackFullException e) {
			System.out.println("overflow");
		}
	}
}

class Stack {
	NodeX top;
	int size;

	Stack(int size) {
		this.size = size;
	}

	NodeX pop() {
		NodeX out = top;
		top = top.next;
		out.next = null;
		return out;
	}

	void push(NodeX in) throws StackFullException {
		int realSize = 0;
		NodeX bottom = null;
		while (top != null) {
			realSize++;
			if (bottom == null) {
				bottom = top;
				top = top.next;
				bottom.next = null;
			} else {
				NodeX currentBottom = bottom;
				bottom = top;
				top = top.next;
				bottom.next = currentBottom;
			}
		}
		if (realSize < size) {
			while (bottom != null) {
				NodeX currentTop = top;
				top = bottom;
				bottom = bottom.next;
				top.next = currentTop;
			}
			System.out.println(top);
			in.next = top;
			top = in;
		} else {
			throw new StackFullException();
		}
	}
}

class NodeX {
	String name;
	NodeX next;

	NodeX(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.name);
		if (this.next != null) {
			sb.append(this.next.toString());
		}
		return sb.toString();
	}
}

class StackFullException extends Exception {
	StackFullException() {
		super();
	}

	StackFullException(String message) {
		super(message);
	}

	StackFullException(String message, Throwable cause) {
		super(message, cause);
	}

	StackFullException(Throwable cause) {
		super(cause);
	}
}

- Mei Li February 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can't seem to delete or edit last answer. Here's the update

//By popping each one to another stack (counting real size), push each one back
public class SizedStack {
	public static void main(String[] args) {
		Stack test = new Stack(5);
		try {
			test.push(new NodeX("a"));
			test.push(new NodeX("b"));
			test.push(new NodeX("c"));
			test.push(new NodeX("d"));
			test.push(new NodeX("e"));
			test.push(new NodeX("f"));
		} catch (StackFullException e) {
			System.out.println("overflow");
		}
	}
}

class Stack {
	NodeX top;
	int size;

	Stack(int size) {
		this.size = size;
	}

	NodeX pop() {
		NodeX out = top;
		top = top.next;
		out.next = null;
		return out;
	}

	void push(NodeX in) throws StackFullException {
		int realSize = 0;
		NodeX bottom = null;
		while (top != null) {
			realSize++;
			if (bottom == null) {
				bottom = top;
				top = top.next;
				bottom.next = null;
			} else {
				NodeX currentBottom = bottom;
				bottom = top;
				top = top.next;
				bottom.next = currentBottom;
			}
		}
		if (realSize < size) {
			while (bottom != null) {
				NodeX currentTop = top;
				top = bottom;
				bottom = bottom.next;
				top.next = currentTop;
			}
			System.out.println(top);
			in.next = top;
			top = in;
		} else {
			throw new StackFullException();
		}
	}
}

class NodeX {
	String name;
	NodeX next;

	NodeX(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.name);
		if (this.next != null) {
			sb.append(this.next.toString());
		}
		return sb.toString();
	}
}

class StackFullException extends Exception {
	StackFullException() {
		super();
	}

	StackFullException(String message) {
		super(message);
	}

	StackFullException(String message, Throwable cause) {
		super(message, cause);
	}

	StackFullException(Throwable cause) {
		super(cause);
	}
}

- Mei Li February 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * How to create an object on the stack.
 * Also make sure that only 5 objects are created for the class
*/

#include <iostream>

using namespace std;

class User {
private:
    int id;
    static int counter;
    static bool isOk;
public:
    User();
    ~User() {}
    int getId() { return id; }
    static int getCounter() { return counter; }
    static bool getStatus() { return isOk; }
    static void resetOk() { isOk = false; }
    static void setOk() { isOk = true; }
};

User::User() {
    if(counter == 5) {
        cout << "Not allowed to create more than 5 objects" << endl;
        resetOk();
        return;
    }
    counter++;
    id = counter;
    setOk();
}

int User::counter = 0;
bool User::isOk = false;

int main()
{
    // Create objects on stack
    User user1;
    (User::getStatus()) ? cout << "user1 id: " << user1.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user2;
    (User::getStatus()) ? cout << "user2 id: " << user2.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user3;
    (User::getStatus()) ? cout << "user3 id: " << user3.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user4;
    (User::getStatus()) ? cout << "user4 id: " << user4.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user5;
    (User::getStatus()) ? cout << "user5 id: " << user5.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user6;
    (User::getStatus()) ? cout << "user6 id: " << user6.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user7;
    (User::getStatus()) ? cout << "user7 id: " << user7.getId() << endl :
                          cout << "Object Construction Failed" << endl;

    return 0;
}

- Glenn Cash March 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * How to create an object on the stack.
 * Also make sure that only 5 objects are created for the class
*/

#include <iostream>

using namespace std;

class User {
private:
    int id;
    static int counter;
    static bool isOk;
public:
    User();
    ~User() {}
    int getId() { return id; }
    static int getCounter() { return counter; }
    static bool getStatus() { return isOk; }
    static void resetOk() { isOk = false; }
    static void setOk() { isOk = true; }
};

User::User() {
    if(counter == 5) {
        cout << "Not allowed to create more than 5 objects" << endl;
        resetOk();
        return;
    }
    counter++;
    id = counter;
    setOk();
}

int User::counter = 0;
bool User::isOk = false;

int main()
{
    // Create objects on stack
    User user1;
    (User::getStatus()) ? cout << "user1 id: " << user1.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user2;
    (User::getStatus()) ? cout << "user2 id: " << user2.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user3;
    (User::getStatus()) ? cout << "user3 id: " << user3.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user4;
    (User::getStatus()) ? cout << "user4 id: " << user4.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user5;
    (User::getStatus()) ? cout << "user5 id: " << user5.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user6;
    (User::getStatus()) ? cout << "user6 id: " << user6.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user7;
    (User::getStatus()) ? cout << "user7 id: " << user7.getId() << endl :
                          cout << "Object Construction Failed" << endl;

    return 0;
}

- Glenn Cash March 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * How to create an object on the stack.
 * Also make sure that only 5 objects are created for the class
*/

#include <iostream>

using namespace std;

class User {
private:
    int id;
    static int counter;
    static bool isOk;
public:
    User();
    ~User() {}
    int getId() { return id; }
    static int getCounter() { return counter; }
    static bool getStatus() { return isOk; }
    static void resetOk() { isOk = false; }
    static void setOk() { isOk = true; }
};

User::User() {
    if(counter == 5) {
        cout << "Not allowed to create more than 5 objects" << endl;
        resetOk();
        return;
    }
    counter++;
    id = counter;
    setOk();
}

int User::counter = 0;
bool User::isOk = false;

int main()
{
    // Create objects on stack
    User user1;
    (User::getStatus()) ? cout << "user1 id: " << user1.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user2;
    (User::getStatus()) ? cout << "user2 id: " << user2.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user3;
    (User::getStatus()) ? cout << "user3 id: " << user3.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user4;
    (User::getStatus()) ? cout << "user4 id: " << user4.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user5;
    (User::getStatus()) ? cout << "user5 id: " << user5.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user6;
    (User::getStatus()) ? cout << "user6 id: " << user6.getId() << endl :
                          cout << "Object Construction Failed" << endl;
    User user7;
    (User::getStatus()) ? cout << "user7 id: " << user7.getId() << endl :
                          cout << "Object Construction Failed" << endl;

    return 0;
}

- glcash57 March 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Program
{
static void Main(string[] args)
{
Stack<Singleton1> singleton1=new Stack<Singleton1>();
for(int i=0;i<10;i++)
{
singleton1.Push(Singleton1.GetInstance(i));
}

}
}
public class Singleton1
{
private const int size = 4;
private static readonly object padlock = new object();
private Singleton1()
{

}

private static Singleton1[] instances = new Singleton1[size];

private static bool initiated;

public static Singleton1 GetInstance(int index)
{
if (index >= size)
return null;
// TryInitiate();

if ( instances[index] == null)
{
lock (padlock)
{

instances[index] = new Singleton1();
}
}

return instances[index];
}


}

- Anonymous April 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Program
    {
        static void Main(string[] args)
        {
            Stack<Singleton1> singleton1=new Stack<Singleton1>();
          for(int i=0;i<10;i++)
            {
                singleton1.Push(Singleton1.GetInstance(i));
            }
            
        }
    }
    public class Singleton1
    {
        private const int size = 4;
        private static readonly object padlock = new object();
        private Singleton1()
        {
           
        }

        private static Singleton1[] instances = new Singleton1[size];

        private static bool initiated;

        public static Singleton1 GetInstance(int index)
        {
            if (index >= size)
                return null;
            // TryInitiate();

            if ( instances[index] == null)
            {
                lock (padlock)
                {
                    
                    instances[index] = new Singleton1();
                }
            }

            return instances[index];
        }

      
    }

- Anonymous April 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

please re-phrase the question as it is not clear with stack, you mean stack memory of jvm or user defined stack data structure

- machau May 18, 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