Ola Cabs Interview Question for SDE-3s


Country: United States
Interview Type: In-Person




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

CustomStack.java

public class CustomStack {
	private String name;
	private int sizeOfStack;
	private int startIndex;
	private int endIndex;
	private int currentIndex;
	
	public CustomStack(String nameOfStack, int sizeOfStack) {
		this.name = nameOfStack;
		this.sizeOfStack = sizeOfStack;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the sizeOfStack
	 */
	public int getSizeOfStack() {
		return sizeOfStack;
	}

	/**
	 * @param sizeOfStack the sizeOfStack to set
	 */
	public void setSizeOfStack(int sizeOfStack) {
		this.sizeOfStack = sizeOfStack;
	}

	/**
	 * @return the startIndex
	 */
	public int getStartIndex() {
		return startIndex;
	}

	/**
	 * @param startIndex the startIndex to set
	 */
	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}

	/**
	 * @return the endIndex
	 */
	public int getEndIndex() {
		return endIndex;
	}

	/**
	 * @param endIndex the endIndex to set
	 */
	public void setEndIndex(int endIndex) {
		this.endIndex = endIndex;
	}

	/**
	 * @return the currentIndex
	 */
	public int getCurrentIndex() {
		return currentIndex;
	}

	/**
	 * @param currentIndex the currentIndex to set
	 */
	public void setCurrentIndex(int currentIndex) {
		this.currentIndex = currentIndex;
	}
}

MultipleStacks.java

import java.util.HashMap;
import java.util.Map;

import com.amazon.exercise.multiplestacks.usercase.StackSizeExceededException;

public class MultipleStacks {

	private int size;
	private int[] completeArray;
	private Map<String, CustomStack> stackMetaMap = new HashMap<String, CustomStack>(1);
	
	public MultipleStacks(CustomStack[] customStackArray) {
		
		int startIndex = 0;
		
		for (CustomStack customStack : customStackArray) {
			size += customStack.getSizeOfStack();
			customStack.setStartIndex(startIndex);
			customStack.setEndIndex(startIndex + customStack.getSizeOfStack() - 1);
			stackMetaMap.put(customStack.getName(), customStack);
		}
		
		completeArray = new int[size];
	}
	
	/**
	 * @param stackName
	 * @param element
	 */
	public void push(String stackName, int element) throws StackSizeExceededException {
		CustomStack customStack = stackMetaMap.get(stackName);
		int currentIndex = customStack.getCurrentIndex();
		
		if (currentIndex <= customStack.getEndIndex()) {
			completeArray[currentIndex] = element;
			customStack.setCurrentIndex(++currentIndex);
		} else {
			throw new StackSizeExceededException("The Size of Stack is exceeded.");
		}
	}
	
	/**
	 * @param stackName
	 * @return
	 */
	public int pop(String stackName) {
		
		CustomStack customStack = stackMetaMap.get(stackName);
		int currentIndex = customStack.getCurrentIndex();
		int element = 0;
		
		if (currentIndex >= customStack.getStartIndex()) {
			currentIndex --;		
			element = completeArray[currentIndex];
			customStack.setCurrentIndex(currentIndex);
		} else {
			System.out.println("Stack Overflow");
			System.exit(1);
		}
		
		return element;
	}
}

StackSizeExceededException.java
----------------------------------------------

public class StackSizeExceededException extends Exception {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1254778790969950293L;
	
	public StackSizeExceededException(String exceptionMessage) {
		super(exceptionMessage);
	}

}

MultipleStacksTest.java

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

public class MultipleStacksTest {
	
	private MultipleStacks multipleStacks;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		
		CustomStack[] customStackArray = new CustomStack[] {
			new CustomStack("A", 2),
			new CustomStack("B", 4),
			new CustomStack("C", 3),
			new CustomStack("D", 5),
		};		
		
		multipleStacks = new MultipleStacks(customStackArray);
	}

	@Test
	public void testPushAndPopToStackA() {
		try {
			multipleStacks.push("A", 1);
			multipleStacks.push("A", 2);
			assertEquals(multipleStacks.pop("A"), 2);
			assertEquals(multipleStacks.pop("A"), 1);
		} catch (StackSizeExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test(expected=StackSizeExceededException.class)
	public void testPushToStackAWhichIsAlreadyCompletelyFilled() {
		try {
			multipleStacks.push("A", 3);
		} catch (StackSizeExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void testPushAndPopToStackB() {
		try {
			multipleStacks.push("B", 4);
			multipleStacks.push("B", 5);
			multipleStacks.push("B", 6);
			multipleStacks.push("B", 7);
			
			assertEquals(multipleStacks.pop("B"), 7);
			assertEquals(multipleStacks.pop("B"), 6);
			assertEquals(multipleStacks.pop("B"), 5);
			multipleStacks.push("B", 8);
			assertEquals(multipleStacks.pop("B"), 4);
		} catch (StackSizeExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

- Anonymous February 14, 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