Amazon Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

I believe this can be done in constant time if M comes in sorted order. Please, brief about this clarification.

- Popeye May 29, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

M elements can be in any order.

- Mit25 June 05, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if M doesn't comes is in sorted order, Is there a way to do it in constant time ?

- sai June 02, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it is simple implementation problem.

- Mit25 June 05, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I came up with this solution.Is there anywhere I can test my solution.Could be a blunder. Any edits are welcome.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Solution {
    public static void main(String... args) {
        Scanner in = new Scanner(System.in);
        String line1 = in.nextLine();
        String[] line1Args = line1.split(" ");
        int n = Integer.valueOf(line1Args[0]);
        int m = Integer.valueOf(line1Args[1]);
        ArrayList<Integer> setValues = new ArrayList<>();
        for (int looper = 0; looper < m; looper++) {
            int temp = in.nextInt();
            setValues.add(temp);
            Collections.sort(setValues);
            int sum = 0;
        for(int i = 0;i<setValues.size();i++){
            if(i == 0){
                if(setValues.size() == 1){
                    sum+=1+n;
                    break;
                }else{
                    sum = 1+(setValues.get(i+1)-1);
                }
            }
            else if(i == setValues.size()-1){
                sum+= (setValues.get(i-1)+1)+n;
            }
            else{
                sum+=(setValues.get(i-1)+1)+(setValues.get(i+1)-1);
            }
        }

        System.out.println(sum);
        }
    }
}

- Klepto June 10, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorting setValues at every insertion is very expensive. Ordered_Set could be used for inserting new element at logn and iterate that with O(n) complexity.

- Ashish Gopal Hattimare July 08, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Solution {
    public static void main(String... args) {
        Scanner in = new Scanner(System.in);
        String line1 = in.nextLine();
        String[] line1Args = line1.split(" ");
        int n = Integer.valueOf(line1Args[0]);
        int m = Integer.valueOf(line1Args[1]);
        ArrayList<Integer> setValues = new ArrayList<>();
        for (int looper = 0; looper < m; looper++) {
            int temp = in.nextInt();
            setValues.add(temp);
            Collections.sort(setValues);
            int sum = 0;
        for(int i = 0;i<setValues.size();i++){
            if(i == 0){
                if(setValues.size() == 1){
                    sum+=1+n;
                    break;
                }else{
                    sum = 1+(setValues.get(i+1)-1);
                }
            }
            else if(i == setValues.size()-1){
                sum+= (setValues.get(i-1)+1)+n;
            }
            else{
                sum+=(setValues.get(i-1)+1)+(setValues.get(i+1)-1);
            }
        }

        System.out.println(sum);
        }
    }

}

- Anonymous June 10, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Solution {
    public static void main(String... args) {
        Scanner in = new Scanner(System.in);
        String line1 = in.nextLine();
        String[] line1Args = line1.split(" ");
        int n = Integer.valueOf(line1Args[0]);
        int m = Integer.valueOf(line1Args[1]);
        ArrayList<Integer> setValues = new ArrayList<>();
        for (int looper = 0; looper < m; looper++) {
            int temp = in.nextInt();
            setValues.add(temp);
            Collections.sort(setValues);
            int sum = 0;
        for(int i = 0;i<setValues.size();i++){
            if(i == 0){
                if(setValues.size() == 1){
                    sum+=1+n;
                    break;
                }else{
                    sum = 1+(setValues.get(i+1)-1);
                }
            }
            else if(i == setValues.size()-1){
                sum+= (setValues.get(i-1)+1)+n;
            }
            else{
                sum+=(setValues.get(i-1)+1)+(setValues.get(i+1)-1);
            }
        }

        System.out.println(sum);
        }
    }
}

- Klepto June 10, 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