Flipkart Interview Question for SDE-2s


Country: United States




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

import numpy as np

(ni, nj) = (3,3)
js = np.ones([ni, nj], dtype = 'int')
k = 3
start = (0,0)
destination = (0, nj - 1)

def calculate_descendants(current, k, ni, nj):
    for i in range(max(current[0] - k, 0), min(current[0] + k, ni) + 1):
        for j in range(current[1], min(current[1] + k, nj) + 1):
            if abs(i - current[0]) + abs(j - current[1]) == k:
                yield (i, j)

def minimum_path(start, destination, js, ni, nj):
    current = start
    discovered = [current]
    processed = list()
    parent = {current: None}
    done = False
    while discovered != [] and not done:
        current = discovered.pop(0)
        processed.append(current)
        for descendant in calculate_descendants(current, js[current[0], current[1]], ni - 1, nj - 1):
            if descendant not in processed: 
                discovered.append(descendant)
                parent[descendant] = current
                if descendant == destination:
                    done = True
    if done:
        current = destination
        path = [current]
        while parent[current] != None:
            current = parent[current]
            path.insert(0, [current])
        print path
    else:
        print "path not found"

minimum_path(start, destination, js, ni, nj)

- Theofilos Strinopoulos June 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please explain the logic?

- NoNameKid June 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

please tell me for that situation can dx or dy be negative as it'll follow dx+dy=k,
i.e dy will be more than k and dx will negative of difference of dy and k.

- vishgupta92 August 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Imagine the graph where the vertices are the stones and the edges connect two stones that we can jump. Perform breadth first search, until you hit the destination point.

- Theofilos Strinopoulos July 18, 2015 | 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