## Facebook Interview Question for Developer Program Engineers

Country: India

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

Give the order of letters and sum integers up followed, right? But how come the 'D' is here?

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

Considering how poor the question was formulated with all those typos I assume the last 'A' is a typo and it should be 'CAE2W3D'. Btw, it can be solved in linear time: assume only upper-case characters and numbers. Iterate over input and count occurrences of letters (~ bucket sort); plus maintain a sum variable for cases a number is encountered. At the end go over occurrences array and at the end append sum.

``````public static void main(String[] args) {
assert "ACDEW5".equals(sort("CAE2W3D"));
}

private static String sort(String str) {
if (str.length() == 0) {
return str;
}
int[] occs = new int[26];
int sum = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
occs[ch - 'A']++;
} else {
sum += ch - '0';
}
}

StringBuilder sb = new StringBuilder();
for (int i = 0; i < occs.length; i++) {
char ch = (char) ('A' + i);
while (occs[i] > 0) {
sb.append(ch);
occs[i]--;
}
}
if (sum > 0) {
sb.append(sum);
}

return sb.toString();
}``````

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

howz this ?

``````import java.util.*;
import java.io.*;

public class prac
{
public static void main(String[] args)
{
String s = "CAE2W3D";
char[] arr = s.toCharArray();
Arrays.sort(arr);
int sum = 0;
for (int i=0;i<arr.length ;i++ )
{
if(Character.isLetter(arr[i]))
{
System.out.print(arr[i]);
}
else
{
sum = sum +  Character.getNumericValue(arr[i]);
}
}
System.out.print(sum);
System.out.println();
}
}``````

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

Or may be this ?

``````import java.util.*;
import java.io.*;

public class prac
{
public static void main(String[] args)
{
String s = "CAE2W3D";
char[] arr = s.toCharArray();
Arrays.sort(arr);
int sum = 0;
int pos = 0;
for (int i=0;i<arr.length ;i++ )
{
if(Character.isLetter(arr[i]))
{
arr[pos] = arr[i];
pos++;
}
else
{
sum = sum +  Character.getNumericValue(arr[i]);
}
}
for (int i=0;i<pos ;i++)
{
System.out.print(arr[i]);
}
System.out.print(sum);
System.out.println();
}
}``````

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

Use an integer array of size 26 for 26 alphabets (whichever is required)
Keep marking (or adding for repititions) the characters at the right position as you visit them. (Neglect non-alphabets)
Print the characters in the array by iterating through its index while you also count for the number of set bits. Append with the counter.

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

Can you explain the problem clearly?

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

Hi,
I think this could be solved by taking a array of 27 elements. Put alphabets in according their ranks that will remove the duplicates. and any number add it with the 27th elements.
Print the whole array till 26th if element is not '0' and print the 27th element as a integer.

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

can't we use the insertion sort with the alphabets and add the numbers if it comes anywhere in string..

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

public class StringAsAlphabetFB {

void sortAndSum(String str) {
int[] array = new int[26];
int sum = 0;
for (int i=0; i<26;i++)
array[i] = 0;

for (int i=0; i<str.length(); i++) {
str = str.toLowerCase();
char c = str.charAt(i);
if (c>='a' && c<='z') {
array[c-'a']++;
}
else if (c>='0' && c<='9') {
sum += Integer.parseInt(c+"");
}
}

for (int i=0; i<26; i++) {
while(array[i]-- > 0)
System.out.print((char) ('a' + i));
}
System.out.println(sum);
}

public static void main(String[] args) {

StringAsAlphabetFB s = new StringAsAlphabetFB();
s.sortAndSum("zertyAB4ER69");

}

}

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

won't the while loop here loop endlessly?

``````for (int i=0; i<26; i++) {
while(array[i]-- > 0)
System.out.print((char) ('a' + i));
}``````

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

Considering the input CAE2W3D, the following code gives the output as expected. This code works for any ASCII character other than alphabet.

``````private static void sortStringAndSumInt(String str) {

int[] charSet = new int[256];
int sum = 0;

for(int i=0; i<charSet.length; i++) {

charSet[i] = 0;
}

for(int i=0; i<str.length(); i++) {

int val = str.charAt(i);

if(!(val >= '0' && val <= '9')) {
charSet[val]++;
}
else {
sum = sum + (val-'0');
}

}

for(int i=0; i<charSet.length; i++) {

if(charSet[i] != 0) {
System.out.print((char)i);
}
}

System.out.print(sum);
}``````

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

what if the string have duplicate letters.i think this code would not works for this case

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

Considering the input CAE2W3D, the following code gives the output as expected. This code works for any ASCII character other than alphabet.

``````private static void sortStringAndSumInt(String str) {

int[] charSet = new int[256];
int sum = 0;

for(int i=0; i<charSet.length; i++) {

charSet[i] = 0;
}

for(int i=0; i<str.length(); i++) {

int val = str.charAt(i);

if(!(val >= '0' && val <= '9')) {
charSet[val]++;
}
else {
sum = sum + (val-'0');
}

}

for(int i=0; i<charSet.length; i++) {

if(charSet[i] != 0) {
System.out.print((char)i);
}
}

System.out.print(sum);
}``````

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

{{
write a program to print the given string as alphabets in order next integres fallowed by sum

example: CAE2W3D is input and output should be

ACDEW5

void PrintStrAndNumSum(char *str)
{
int n = A.Length;
int offset = (int)'A';
int map[27];

for(int i = 0 ; i<27 ;i++)
map[i] = 0;

while(*str != '/0')
{
if(*str >= 'A' && *str <= 'Z')
map[(int)(*str - 'A')]++;
else
if(*str >='0' && *str <= '9')
map[26] += (int) (*str - '0');
else
{
// Charset is out of range
return -1;
}
}

for(int i = 0 ; i<26 ;i++)
{
while(map[i] > 0)
{
print('%c' , i + 'A');
map[i]--;
}
}

print("%d",map[26]);

}

}
}}

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

{{
write a program to print the given string as alphabets in order next integres fallowed by sum

example: CAE2W3D is input and output should be

ACDEW5

void PrintStrAndNumSum(char *str)
{
int n = A.Length;
int offset = (int)'A';
int map[27];

for(int i = 0 ; i<27 ;i++)
map[i] = 0;

while(*str != '/0')
{
if(*str >= 'A' && *str <= 'Z')
map[(int)(*str - 'A')]++;
else
if(*str >='0' && *str <= '9')
map[26] += (int) (*str - '0');
else
{
// Charset is out of range
return -1;
}
}

for(int i = 0 ; i<26 ;i++)
{
while(map[i] > 0)
{
print('%c' , i + 'A');
map[i]--;
}
}

print("%d",map[26]);

}

}
}}

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

``````void FacebookTest1()
{
const char const szInput[] = "CAE2W3A";
char tmp[26] = {0x00};
size_t sum = 0;

for( size_t i = 0; i < sizeof(szInput); i++ )
{
char chr = szInput[i];
size_t index = chr - 'A';

if( '0' <= chr && chr <= '9' )
{
sum += chr - '0';
}
else
{
if( 0x00 == tmp[index] )
{
tmp[index] = szInput[i];
}
}
}
for( size_t i = 0; i < 26; i++ )
{
if( tmp[ i ] )
{
cout << tmp[ i ];
}
}
cout << sum << endl;
}``````

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

First partition on 'A', based on ascii value of these characters.. that should return the index of 'A'.
and then right side will be alphabet, left would be numerics.. quick_sort the alphabet part (index of 'A' ...end)...and add the numerics (0.. index of 'A') and move to the end.

``````char a[] = {'C','D','E','2','W','3','A' };
char pivot = 'A',tmp;
int idx = 0, val = 0;
for(int i = 0;i<a.length;i++){
if(a[i]<pivot){
tmp = a[i];
a[i] = a[idx];
a[idx] = tmp;
idx++;
}
}
Arrays.sort(a, idx, a.length);
for(int i=0;i<idx;i++) val+=(a[i]-'0');
String ans = new String(a,idx,a.length-idx)+val;

System.out.println(" "+ ans);``````

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

it is better to use linked list to store characters in sorted form am i correct?

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

``````#include "stdio.h"
#include "string.h"
#include "malloc.h"
#define NUMBER_OF_ALPHABET 27
char * reorder(char * str) {
int i,j =0;
int * a =(int *) calloc(sizeof(int), NUMBER_OF_ALPHABET + 1);
char * s = (char *) calloc(sizeof(char), strlen(str));
for(i = 0; i < strlen(str); i ++) {
if ((int)str[i] <= 'Z' & (int)str[i] >='A'){
int index = (int) str[i] - 'A';
if (index >=0 && index <= NUMBER_OF_ALPHABET)
a[index]++;
}
else if(str[i]>='0' && str[i]<='9')
a[NUMBER_OF_ALPHABET] += (int)str[i] - '0';
}
for(i = 0; i< NUMBER_OF_ALPHABET; i++) {
while (a[i] > 0) {
s[j] = (char) (i + 'A');
j++;
i++;
}
}
if(a[NUMBER_OF_ALPHABET] > 0)
s[j] = (char) (a[NUMBER_OF_ALPHABET] + '0');
return s;
}
int main () {
char * test_string = "RADOE3F2";
printf("The original string is %s.\nThe ordered string is %s\n",
test_string,reorder(test_string));
return 0;
}``````

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

#include<iostream>
#include<conio.h>
#include<vector>
#include<algorithm>
#include<string>
#include<iterator>
#include<cstdlib>
#include<map>
#include<numeric>
using namespace std;

bool palindrome(string s)
{
return equal(s.begin(),s.end(),s.rbegin());
}
int main()
{
string kratos;
vector<int> vec;
kratos="nare12sh";
string::const_iterator iter;
for(iter=kratos.begin();iter!=kratos.end();iter++)
{
char p;
p=*iter;
char* g=&p;
int f=atoi(g);
if(f!=0)
vec.push_back(f);
}
int d=accumulate(vec.begin(),vec.end(),0);
cout<<d;
getch();
return 0;

}

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

``````#include <iostream>
#include <string>
using namespace std;

int main()
{
string inputStr;
string numericStr;
string alphaStr;
int numericStrLen = 0;
int alphaStrLen = 0;

cout << "Enter your string " << endl;
cin >> inputStr;

for (int i=0; i<inputStr.length(); i++)
{
if( toascii(inputStr.at(i)) < 58)
{
int j=0;
while(j<numericStr.length() && numericStr.at(j)<inputStr.at(i))
{
j++;
}
numericStr.insert(j,1,inputStr.at(i));
}
else
{
int j=0;
while(j<alphaStr.length() && alphaStr.at(j)<inputStr.at(i))
{
j++;
}
alphaStr.insert(j,1,inputStr.at(i));
}
}
cout << endl << alphaStr << numericStr << endl;
return 1;
}``````

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

``````void sort_alpha_sum_int(const char *str)
{
int ref_counts[256];
int sum;
int i;

memset(ref_counts, 0, sizeof(ref_counts));
while (*str != '\0') {
if (*str >= '0' && *str <= '9') {
sum += (int)(*str - '0');
} else {
ref_counts[(unsigned char)*str]++;
}
}
for (i = 0; i < 256; ++i) {
while(ref_counts[i]--) {
printf("%c", (char)i);
}
}
printf("%d\n", sum);
}``````

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

``````def main():
a = raw_input();
string = ""
su = 0
for i in a:
c = ord(i)
if c > 47 and c < 58:
su += int(i);
else:
string += str(i);
string = "".join(sorted(string))
print string+str(su);
main()``````

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