Amazon Interview Question for SDE-2s


Country: United States




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

void printcomma(int n)
if ( n < 0)
{
printf("-");
printcomma(-n);
return;
}

if(n < 1000) {
printf("%d", n);
return;
}

printcomma(n/1000);
printf(",%03d", n%1000);

}

- prathap July 25, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

number=123456
number='{:,}'.format(number)

- prathap July 25, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ num = 1002490779 print("{:,}".format(num)) }} - manasa August 09, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Space: O(n)
Running time: O(n)

public static String printCommas(String str) {
        final StringBuilder builder = new StringBuilder();

        for(int i=str.length()-1,group=1; i>=0; i--, group++){
            builder.append(str.charAt(i));

            if(group % 3 == 0) {
                builder.append(",");
                group=0;
            }
        }

        return builder.reverse().toString();
    }

- Yev August 26, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static String commaSeparate(int num) {
if(num<0){
return "-"+commaSeparate(-num);
}else if(num<1000){
return num+"";
}else{
String gap="";
int rem = (int)num%1000;
if(rem<10) gap="00";
else if(rem<100) gap ="0";
return commaSeparate(num/1000)+","+gap+rem;
}
}

- Anit September 20, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static String commaSeparate(int num) {
        if(num<0){
            return "-"+commaSeparate(-num);
        }else if(num<1000){
            return num+"";
        }else{
            String gap="";
            int rem = (int)num%1000;
            if(rem<10) gap="00";
            else if(rem<100) gap ="0";
            return commaSeparate(num/1000)+","+gap+rem;
        }
    }

- Anit September 20, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static String commaSeparate(int num) {
        if(num<0){
            return "-"+commaSeparate(-num);
        }else if(num<1000){
            return num+"";
        }else{
            String gap="";
            int rem = (int)num%1000;
            if(rem<10) gap="00";
            else if(rem<100) gap ="0";
            return commaSeparate(num/1000)+","+gap+rem;
        }

}

- Anit September 20, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static String commaSeparate(int num) {
        if(num<0){
            return "-"+commaSeparate(-num);
        }else if(num<1000){
            return num+"";
        }else{
            String gap="";
            int rem = (int)num%1000;
            if(rem<10) gap="00";
            else if(rem<100) gap ="0";
            return commaSeparate(num/1000)+","+gap+rem;
        }

}

- anit September 20, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static String commaSeparate(int num) {
        if(num<0){
            return "-"+commaSeparate(-num);
        }else if(num<1000){
            return num+"";
        }else{
            String gap="";
            int rem = (int)num%1000;
            if(rem<10) gap="00";
            else if(rem<100) gap ="0";
            return commaSeparate(num/1000)+","+gap+rem;
        }
}

- anit September 20, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String commaSeparate(int num){
	if(num<0){
		return "-"+commaSeparate(-num);
	}else if(num<1000){
		return num+"";
	}else{
		String gap="";
		int rem= num%1000;
		if(rem<10) gap="00";
		else if(rem<100) gap="0";
		return commaSeparate(num/1000)+","+gap+rem;
	}
    }
}

- anit September 20, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String returnWithCommas (String givenString) {

    int digits = 0;

    for (int i=givenString.length()-1; i>0; i--) {
      digits ++;
      if (digits % 3 == 0) {
        givenString = givenString.substring(0, i) + "," + Character.toString(givenString.charAt(i)) + givenString.substring(i+1, givenString.length());
      }
    }

    return givenString;
  }

- Nightbridge October 03, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// iterative : add commas from the left most while counting the number of commas added.
// time O(n) space O(1) where n is the number of characters in a given string s
public static String returnWithCommas (String s) {
 if (s == null || s.length() <= 3) {
  return s;
 }

 int countCommas = 0;
 int remain = s.length() % 3;
 int strIdx = 0;

  s = s.substring(strIdx, strIdx + remain) + "," + s.substring(strIdx + remain);
  ++countCommas;
  strIdx += (remain + countCommas);

 while (strIdx < s.length() - 1) {
  s = s.substring(strIdx, strIdx + 3) + "," + s.substring(strIdx + 3);
  ++countCommas;
  strIdx += 3;
 }

 return s;
}

// recursive : recursively chop the string up every three characters and add a commas
// time O(n) space O(1)
public static String returnWithCommas (String s) {
 if (s == null || s.length() < 4) {
  return s;
 }

 int remain = s.length() % 3;

 return s.substring(0, remain) + "," + returnWithCommasUtil(s, remain);
}

public static String returnWithCommasUtil (String s, int strIdx) {
 if (strIdx >= s.length() - 1) {
  return s.substring(strIdx);
 }

 return s.substring(strIdx, 3) + "," + returnWithCommasUtil(s, strIdx + 3);
}

- Euihoon Seol October 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class T3 {

	private static String input = "1010503";
	private static String result = new String();

	public static void main(String[] args) {
		int count = 0;
		processing(count);
		System.out.println(result);
	}

	private static void processing(int count) {

		if (count > input.length() - 1)
			return;
		else {
			if ((count % 3) == 0) {
				result += input.charAt(count);
				if (count != input.length() - 1) {
					result += ",";
				}
			} else {
				result += input.charAt(count);
			}
			count++;
			processing(count);
		}
	}
}

- mannerh1 October 24, 2020 | 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