Hackerrank Interview Question for Senior Software Development Engineers


Country: India
Interview Type: Written Test




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

A possible optimization is to check if the resolution matrix already has computed a solution and add that value instead computing all the possible paths when adding a new element to the goals list

def visiting_matrix(matrix):
    def compute_successors(x, y):
        res = []
        for x1, y1 in [ (0, 1), (1, 0) ]:
            x2 = x + x1
            y2 = y + y1
            if x2 >= len(matrix): x2 -= len(matrix)
            if y2 >= len(matrix): y2 -= len(matrix)
            res.append((x2, y2))
        return res
        
    r = [ [ 0 ] * len(matrix) for _ in xrange(len(matrix)) ]
    positions = [ (x, y) for x in xrange(len(matrix))
                         for y in xrange(len(matrix)) ]
    
    for (x, y) in positions:
        total = 0
        goals = [ (x, y) ]
        for g in goals:
            total += 1
            successors = compute_successors(g[0], g[1])
                              
            for s in successors:
                if matrix[g[0]][g[1]] > matrix[s[0]][s[1]]:
                    goals.append(s)
        r[x][y] = total

    return r

- Fernando May 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def visiting_matrix(matrix):
def compute_successors(x, y):
res = []
for x1, y1 in [ (0, 1), (1, 0) ]:
x2 = x + x1
y2 = y + y1
if x2 >= len(matrix): x2 -= len(matrix)
if y2 >= len(matrix): y2 -= len(matrix)
res.append((x2, y2))
return res

r = [ [ 0 ] * len(matrix) for _ in xrange(len(matrix)) ]
positions = [ (x, y) for x in xrange(len(matrix))
for y in xrange(len(matrix)) ]

for (x, y) in positions:
total = 0
goals = [ (x, y) ]
for g in goals:
total += 1
successors = compute_successors(g[0], g[1])

for s in successors:
if matrix[g[0]][g[1]] > matrix[s[0]][s[1]]:
goals.append(s)
r[x][y] = total

return r

- Anonymous September 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you start from 1 then you can only move to right or down... and you can visit next element only if the next one is smaller ..so on the right of 1 there are 2 and 3 which is bigger so you cant visit and if you go down there are also 2, 3.. so for 1..you can visit only one element which is itself....

1 2 3
1
2

here for 3, you cant move left but can go down, where there are 1 and 2 which are smaller.. so you can visit..so total is 3 itself, 1 and 2.. so you can write 3

- Anonymous September 01, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i didn't understand the question. can anyone explain how the output come as 1 1 3
1 3 1
3 1 1

- kandoi.sumit October 16, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function solution(matrix) {
	function p(row, col, initialValue) {
		if (Math.max(row, col) >= matrix.length || matrix[row][col] >= initialValue) return 0;
		return 1 + Math.max(p(row + 1, col, initialValue), p(row, col + 1, initialValue));
        }
	let result = [];
	for (let r = 0; r < matrix.length; r++){
		result[r] = [];
		for (let c = 0; c < matrix.length; c++){
			result[r][c] = 1 + Math.max(p(r + 1, c, matrix[r][c]), p(r, c + 1, matrix[r][c]));
                }
        }
	return result;
}

- Anonymous November 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function solution(matrix) {
	function p(row, col, initialValue) {
		if (Math.max(row, col) >= matrix.length || matrix[row][col] >= initialValue) return 0;
		return 1 + Math.max(p(row + 1, col, initialValue), p(row, col + 1, initialValue));
    }
	let result = [];
	for (let r = 0; r < matrix.length; r++){
		result[r] = [];
		for (let c = 0; c < matrix.length; c++){
			result[r][c] = 1 + Math.max(p(r + 1, c, matrix[r][c]), p(r, c + 1, matrix[r][c]));
        }
    }
	return result;
}

- YaBoi November 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function solution(matrix) {
	function p(row, col, initialValue) {
		if (Math.max(row, col) >= matrix.length || matrix[row][col] >= initialValue) return 0;
		return 1 + Math.max(p(row + 1, col, initialValue), p(row, col + 1, initialValue));
    }
	let result = [];
	for (let r = 0; r < matrix.length; r++){
		result[r] = [];
		for (let c = 0; c < matrix.length; c++){
			result[r][c] = 1 + Math.max(p(r + 1, c, matrix[r][c]), p(r, c + 1, matrix[r][c]));
        }
    }
	return result;
}

- yaboi435354 November 07, 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