Oracle Interview Question
SDE-2sCountry: India
Interview Type: In-Person
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.
//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);
}
}
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);
}
}
/*
* 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;
}
/*
* 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;
}
/*
* 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;
}
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];
}
}
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];
}
}
Make the new and new[] private.
- Sunil Raj February 11, 2017Have 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