Adobe Interview Question for Computer Scientists


Country: United States
Interview Type: In-Person




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

// insertion sort can also work well for partially sorted arrays. Below, is a technique that 
// uses k -sized min heaps

  /**
     *
     * @param a a k-sorted array, each element is at most k spaces away from its final position after sorting
     * @param k value of k, k<= array size
     * @return fully sorted array
     */
    static int [] ksort(int[] a, int k) {
        final int windowSize = k + 1;
        if (a.length < windowSize) throw new IllegalArgumentException();
        PriorityQueue<Integer> q = new PriorityQueue<>();
        int i = 0, j = 0;
        //first k + 1 items are put in heap
        for (; i < windowSize; i++) q.add(a[i]);
        // we extra elements from the heap and add them to sorted array
        // for each item lost from the heap, one is added to the heap from the
        // unsorted part of the array
        for (; i < a.length; i++, j++) {
            a[j] = q.poll();
            q.add(a[i]);
        }
        // the right edge of window k+1 elements has merged with the right
        // end of the array. There are no more elements to add. We delete
        // elements from the heap till it is empty
        while (!q.isEmpty()) {
            a[j++] = q.poll();
        }
        return a;
    }

- Makarand August 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@markarand, is that O(nlogn)?

Maybe I'm misinterpreting the problem, if each element is K length away from its sorted position, wouldn't a first easy solution be, find minimum element index, its index is K, then we just have to shift each element K position.

- tnutty2k8 September 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ tnutty2k8 - this is O (n Log k), assuming that k<< N, this is more efficient

- Makarand September 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void sortedArray(int[] arr, int k) {
        
        
        int size = arr.length;
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
        for(int i=0; i<k; i++) {
            queue.add(arr[i]);
        }
        
        int m =0;
        int n = k;
        
        while(!queue.isEmpty()){
            
            Integer value = queue.poll();
            arr[m++] = value;
            if(n < size) {
                queue.add(arr[n++]);
            }
            
        }
        
        System.out.println(" "+Arrays.toString(arr));
        
    }

- Kapil November 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Kapil: You've considered each number is k positions ahead of its appropriate position. It may equally be possible that it's behind its actual position. So in that situation, you code won't work nor Makarand's code.

- Sameer Oak July 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Sameer Oak The solution given by @Makarand is correct. He's building a min heap using the first (k + 1) elements. For the following input, the solution will give the correct output.

2 1 4 3
k = 1

- PraTrick March 23, 2019 | 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