Amazon Interview Question
SDE-2sCountry: United States
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();
}
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;
}
// 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);
}
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);
}
}
}
class Test{
public static void main(String args[]){
String s="1010503";
int n=s.length();
StringBuilder str=new StringBuilder();
int count=1;
for(int i=n-1;i>=0;i--){
if(count%3==0){
str.append(s.charAt(i));
str.append(",");
count++;
}
else{
str.append(s.charAt(i));
count++;
}
}
str.reverse();
String result=str.toString();
System.out.print(result);
}
}
public class test {
public static void main(String args[]) {
String s="1010503";
int n=s.length();
StringBuilder str=new StringBuilder();
int count=1;
for(int i=n-1;i>=0;i--){
if(count%3==0){
str.append(s.charAt(i));
str.append(",");
count++;
}
else{
str.append(s.charAt(i));
count++;
}
}
str.reverse();
String result=str.toString();
System.out.print(result);
}
}
#include <iostream>
#include <string>
std::string formatNumberWithCommas(const std::string& number) {
std::string formattedNumber{};
int count = 0;
const int Len = number.length();
// Iterate from the end of the string
for (int i = Len - 1; i >= 0; --i) {
formattedNumber = number[i] + formattedNumber;
++count;
// Insert a comma every three digits, except at the beginning
if (count % 3 == 0 && i != 0) {
formattedNumber = "," + formattedNumber;
}
}
return formattedNumber;
}
int main() {
std::string number = "1010503";
std::cout << formatNumberWithCommas(number) << std::endl; // Output: 1,010,503
return 0;
}
- prathap July 25, 2020