JP Morgan Interview Question for Associates


Country: United States




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

This is one of the quick solution. Assuming only characters appear in the board be A-Z

import java.util.ArrayList;
import java.util.Hashtable;

public class CheckBoard {

	public static void main(String[] args) {
		CheckBoard checkBoard = new CheckBoard();
		String word = "ABCCED";
		checkBoard.checkIfExists(getBoard(), word);
		word = "SEE";
		checkBoard.checkIfExists(getBoard(), word);
		word = "ABCB";
		checkBoard.checkIfExists(getBoard(), word);

	}
	
	private static char[][] getBoard(){
		char[][] board = {{'A','B','C','E'},
						  {'S','F','C','S'}, 
						  {'A','D','E','E'} 
						};
		return board;
	}

	private void checkIfExists(char[][] board, String word) {
		if (isWordExists(word, constructLists(board, 3, 4))) {
			System.out.println("Exists");
		} else {
			System.out.println("Does not exists");
		}
	}
	
	private boolean isWordExists(String word, Hashtable<Integer, ArrayList<Position>> hashtable) {
		if(word == null){
			return true;
		}
		ArrayList<Position> list = hashtable.get(word.charAt(0)-'A');
		if(list == null){
			return false;
		}
		for (Position position : list) {
			position.traversed = true;
			if(isSubWordExists(word.substring(1),hashtable,position)){
				return true;
			}
			position.traversed = false;
		}
		return false;
	}

	private boolean isSubWordExists(String substring, Hashtable<Integer, ArrayList<Position>> hashtable,
			Position position) {
		if(substring.equals("")){
			return true;
		}
		ArrayList<Position> list = hashtable.get(substring.charAt(0)-'A');
		if(list == null){
			return false;
		}
		if(substring.length() == 1){
			for (Position neghPos : list) {
				if(neghPos.traversed == true){
					continue;
				}
				if(position.x == neghPos.x){
					if(Math.abs(position.y-neghPos.y) == 1){
						return true;
					}
				}else if(position.y == neghPos.y){
					if(Math.abs(position.x-neghPos.x) == 1){
						return true;
					}
				}
			}
			return false;
		}else{
			for (Position neghPos : list) {
				neghPos.traversed = true;
				if(isSubWordExists(substring.substring(1), hashtable, neghPos)){
					return true;
				}
				neghPos.traversed = false;
			}
		}
		
		return false;
	}
	
	public Hashtable<Integer, ArrayList<Position>> constructLists(char[][] board, int length, int width){
		Hashtable<Integer, ArrayList<Position>> occurancePositions = new Hashtable<Integer,ArrayList<Position>>(26);
		for(int i=0;i<length;i++){
			for(int j=0;j<width;j++){
				ArrayList<Position> list = occurancePositions.get(board[i][j] -'A');
				if(list == null){
					list = new ArrayList<Position>();
					occurancePositions.put(board[i][j] -'A', list);
				}
				list.add(new Position(i, j));
			}
		}
		return occurancePositions;
	}
	
	class Position{
		int x;
		int y;
		boolean traversed;
		
		public Position(int x,int y) {
			this.x = x;
			this.y = y;
			traversed = false;
		}
	}

}

- dadakhalandhar March 09, 2018 | 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