Amazon Interview Question
SDETsCountry: United States
Interview Type: Phone Interview
package careerUp;
public class Stack {
private int arr[];
private int maxSize;
private int top = -1;
private int bottom = 0;
Stack(int s){
maxSize = s;
arr = new int[maxSize];
top = -1;
}
public void push(int elm){
arr[++top] = elm;
}
public int pop(){
return arr[top--];
}
public int peekArray(){
return arr[top];
}
public boolean isEmpty(){
return (top == -1);
}
public boolean isFull(){
return (top == maxSize-1);
}
public static void main(String args[]){
Stack stack = new Stack(5);
stack.push(10);
stack.push(12);
stack.push(13);
stack.push(15);
stack.push(14);
while(!stack.isEmpty()){
System.out.println(stack.pop());
}
}
}
package careerUp;
public class Stack {
private int arr[];
private int maxSize;
private int top = -1;
private int bottom = 0;
Stack(int s){
maxSize = s;
arr = new int[maxSize];
top = -1;
}
public void push(int elm){
arr[++top] = elm;
}
public int pop(){
return arr[top--];
}
public int peekArray(){
return arr[top];
}
public boolean isEmpty(){
return (top == -1);
}
public boolean isFull(){
return (top == maxSize-1);
}
public static void main(String args[]){
Stack stack = new Stack(5);
stack.push(10);
stack.push(12);
stack.push(13);
stack.push(15);
stack.push(14);
while(!stack.isEmpty()){
System.out.println(stack.pop());
}
}
}
package Stack;
public class Stack {
private int capacity;
private int[] baseArray;
private int index = -1;
private int size = 0;
public Stack(int capacity) {
this.capacity = capacity;
baseArray = new int[capacity];
}
public void push(int data)throws StackFullException{
if(this.isFull()){
throw new StackFullException("Stack Full ... Can't push more data");
}
baseArray[++index] = data;
size++;
}
public int pop()throws StackEmptyException{
if(this.isEmpty()){
throw new StackEmptyException("Stack Empty ... Nothing to pop ");
}
size--;
return baseArray[index--];
}
public int peek(){
return baseArray[index];
}
public boolean isEmpty(){
if(this.size() < 1){
return true;
}
return false;
}
public boolean isFull(){
if(this.size == this.capacity){
return true;
}
return false;
}
public int size(){
return this.size;
}
}
package Stack;
public class StackImpl {
public static void main(String[] args) throws Exception{
Stack stack = new Stack(5);
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
while(!stack.isEmpty()){
System.out.println(stack.pop());
}
}
}
class Stack:
- data_freak June 07, 2016def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)