Amazon Interview Question for Software Engineer / Developers


Country: India




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

public class KSmallest2 {

    private MinPQ<Integer> minHeap;
    private int x;
    private int k;
    private int count = 0;

    public KSmallest2(String filename, int x, int k) {
        this.x = x;
        this.k = k;
        minHeap = new MinPQ<>();
        try {
            Scanner in = new Scanner(new File(filename));
            while (in.hasNext()) {
                minHeap.insert(in.nextInt());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public boolean check(int index) {

        if (index > minHeap.size()) {
            return false;
        }

        if (minHeap.getByIndex(index) < x) {
            count++;
            if (count >= k) {
                return true;
            }

            return  check(2 * index) ||
                    check(2 * index + 1);
        }

        return false;
    }



    public static void main(String[] args) {
        KSmallest2 ks = new KSmallest2("src/main/resources/minheap.txt", 18, 5);
        System.out.println(ks.minHeap);

        System.out.println(ks.check(1));
    }
}

- Ilya.Rudyak June 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

htt p : // stackoverflow.com/questions/4922648/how-we-can-find-kth-largest-element-from-a-max-heap-in-ok-time

- Anonymous December 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I suppose in this case heap should be min heap.

- Srikant Aggarwal December 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good soln.. to summarize: we just need to traverse a min heap as though it is a BST

- Anonymous December 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry this should be a max heap

- Anonymous December 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The link gives the answer of kth largest from a max heap ... doesn't seem much complicated ... traverse the heap for 1st k values and compare. (would be same for kth smallest in a min-heap) But what if the question was 'kth smallest from max-heap' (a heap by default is max heap unless mentioned otherwise). is it still possible in O(k)? please give an algo if possible

- sb December 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@topcoder99, Are you missing any data point? Array based heap does not make any difference unless it is a max-heap or a min-heap. Otherwise for a general array, finding the Kth smallest element will itself take O(nlogk) time complexity.

- sam December 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are ways to find the k-th smallest element in O(n) time.

- eugene.yarovoi December 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

See "quickselect" or something like that. There are also deterministic versions of that algorithm.

Now how to do it in O(k) time, I'm not really sure...I only have O(k log k).

- eugene.yarovoi December 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think its should be O(k log n)

- Anonymous December 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone explain me how finding k smallest element says x is greater than kth smallest element?

- jeevi November 10, 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