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
Comment hidden because of low score. Click to expand.
0
of 0 vote

Verified solution :

class Solution {
    private static int[][] adj = {{0,1},{0,-1},{1,0},{-1,0}};
    
    public boolean exist(char[][] board, String word) {
        boolean result=false;
        for(int i=0;i<board.length;i++){
			for(int j=0;j<board[i].length;j++){
                Set<String> path = new HashSet<>();
                if(existRecursive(board,word,0,i,j,path)){
                    return true;
                }
            }
        }
        return result;
    }
    
    public boolean existRecursive(char[][] board, String word,int pos,int x, int y,Set<String> path) {
        if(pos>=word.length()){
            return true;
        }
        if(x>=0 && x<board.length 
        && y>=0 && y<board[x].length 
        && !path.contains(x+","+y) && word.charAt(pos)==board[x][y]){
            path.add(x+","+y);
            for(int j=0;j<adj.length;j++){
                int x1=x+adj[j][0];
                int y1=y+adj[j][1];
                if(existRecursive(board,word,pos+1,x1,y1,path)){
                    return true;
                }
            }
            path.remove(x+","+y);
        }
        return false;
    }
}

- mbhat2013 April 13, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package main

import "fmt"

type input struct{
	grid [][]uint8
	word string
	w int
	x int
	y int
}

type gridOps interface{
	isWordPresentInGrid() bool
	SearchInGrid()
	IsRight() bool
	IsLeft() bool
	IsUp() bool
	IsDown()

}

func (ip *input) IsRight() bool{
	if ip.y == len(ip.word)-1{
		return false
	}
	if ip.word[ip.w] == ip.grid[ip.x][ip.y+1]{
		return true
	}
	return false
}

func (ip *input) IsLeft() bool{
	if ip.y == 0{
		return false
	}
	if ip.word[ip.w] == ip.grid[ip.x][ip.y-1]{
		return true
	}
	return false
}

func (ip *input) IsUp() bool{
	if ip.x == 0{
		return false
	}
	if ip.word[ip.w] == ip.grid[ip.x-1][ip.y]{
		return true
	}
	return false
}

func (ip *input) IsDown() bool{
	if ip.x == len(ip.word)-1{
		return false
	}
	if ip.word[ip.w] == ip.grid[ip.x+1][ip.y]{
		return true
	}
	return false
}

func (ip *input) SearchInGrid() bool{
	ip.w = ip.w + 1
	if ip.w < len(ip.word) {
		if ip.IsRight() {
			ip.SearchInGrid()
		}else if ip.IsLeft() {
			ip.SearchInGrid()
		}else if ip.IsUp() {
			ip.SearchInGrid()
		}else if ip.IsDown() {
			ip.SearchInGrid()
		} else{
			//fmt.Println("word not found")
			return false
		}
	}else{
		return true
	}
	return false
}

func (ip *input)isWordPresentInGrid() bool{
	outer:
	for w := 0; w < len(ip.word); w++{
		for i := range ip.grid{
			for j := range ip.grid{
				if ip.grid[i][j] != ip.word[w]{
					continue
				} else{
					if ip.SearchInGrid(){
						break outer
					}
				}
			}
		}
	}
	return false
}

func main() {

	g := [][]uint8{{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}
	ip := input{grid: g, word : "ABCB"}
	if ip.isWordPresentInGrid(){
		fmt.Println("word found")
	}else{
		fmt.Println("word not found")
	}

}

- Nikita15p April 26, 2020 | 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