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.

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

M elements can be in any order.

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 ?

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

I think it is simple implementation problem.

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);
}
}
}``````

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.

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);
}
}``````

}

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);
}
}
}``````

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

#include <stdio.h>

int main()
{
int num,n,sum;
scanf("%d %d",&num,&n);
int arr[n];
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
for(int i=0;i<n;i++)
{
sum=1;
if(i==0)
{
sum+=num;
}
else
{
for(int j=0;j+1<=i;j++)
{
sum+=arr[j+1]+arr[j];
}
sum+=num;
}
printf("%d ",sum);
}
return 0;
}

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

No need to sort

result = 1 + N + 2 * sum(x[i]) - min(x[i]) - max(x[i])

Example:
N = 10
x = {2,5,7,9}

1 + N = 11
sum(x[i]) = 2 + 5 + 7 + 9 = 23
min = 2
max = 9

result = 11 + 2 * 23 - 2 - 9 = 46

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

Great anwser, thanks!

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