Apple Interview Question for SDE-3s


Country: United States
Interview Type: In-Person




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

/* *********** 1. Sorting the stack values, Here you can find the Java program for this problem************* */

/* *********** 1. Sorting the stack values, Here you can find the Java program for this problem************* */

package com.kishore.samples;

import java.util.Stack;

public class StackSort {
 
    public static Stack<Integer> sortStack(Stack<Integer> input){
         
        Stack<Integer> tmpStack = new Stack<Integer>();
         while(!input.isEmpty()) {
            int tmp = input.pop();
            System.out.println("Element taken out: "+tmp);
            while(!tmpStack.isEmpty() && tmpStack.peek() > tmp) {
                input.push(tmpStack.pop());
            }
            tmpStack.push(tmp);
            System.out.println("input: "+input);
            System.out.println("tmpStack: "+tmpStack);
        }
        return tmpStack;
    }
     
    public static void main(String a[]){
         
        Stack<Integer> input = new Stack<Integer>();
        input.add(34);
        input.add(3);
        input.add(31);
        input.add(98);
        input.add(92);
        input.add(23);
        System.out.println("input: "+input);
        System.out.println("final sorted list: "+sortStack(input));
    }
}

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

/* // O(n) and no extra space
* Solution is same as in case when we have 0 to n-1 element in size n array
* In here if any arr[i] > size of arr ,we do not mark its index as negative since that
* index is not in array. but mark of others.
* Also, shift all negative numbers to the right of array and clip the array
*
*
*/

import java.util.*;

public class MinMissingInteger {
	
	public static void findMinMissing(int[] arr){
		//Move -ve number to end of array either shift or just remove
		int ctr=0;
		for(int i=0;i<=arr.length-1;i++){
			if(arr[i]>0){
				arr[ctr] = arr[i];
				ctr++;
			}
		}
		
		//array with positive numbers is till ctr-1
		int maxEndLength=ctr-1; // max length of positive numbers
		
		//Mark kth index where k=arr[i]  and is in array's range
		//Since +ve numbers start from 1 and array index from 0, we mark k-1 index as negative
		
		for(int i=0;i<=maxEndLength;i++){
			int index = Math.abs(arr[i])-1; // minus 1, element may be marked negative so check abs value
			if(index <= maxEndLength){
				arr[index] = -1*arr[index];
			}
		}
		
		//find the first missing positive number
		int firstMissingIndex=0;
		for(int i=0;i<=maxEndLength;i++){
			if(arr[i]>0){
				firstMissingIndex=i+1; // since we subtracted 1
				break;
			}
		}

		System.out.println("Missing value = " + firstMissingIndex);
	}

	public static void main(String[] args) {
		int[] arr = {1,9,2,5,7};
		findMinMissing(arr);
	}
}

- Mayank Jain August 21, 2017 | 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