Salesforce Interview Question for Developer Program Engineers


Country: India




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

copy the whole string at the end and start copying char by char to from the begining.. when space occurred add "ABC" to the string..

here is the program...

void duplicate(char *str,int len)
{
char *strEnd = str + strlen(str);

while(strEnd != str)
{
*(str + len) = *strEnd;
len--;
*strEnd-- = '\0';
}

*(str + len ) = *strEnd;
*strEnd = '\0';

strEnd = str + len;

while(*strEnd)
{
if(*strEnd == ' ')
{
*str++ = 'A';
*str++ = 'B';
*str++ = 'C';
}
else
{
*str++ = *strEnd;
*strEnd = '\0';
}
strEnd++;
}
*str = '\0';
}

- Anonymous September 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static String replace(String replacer,String inp)
	{
		String[] str=inp.split(" ");
		inp="";
		for(int i=0;i<str.length;i++)
		{
			if(i!=str.length-1)
			str[i]=str[i]+replacer;
			inp=inp+str[i];
		}
		return inp;
	}

- kulkarniaks007 March 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey52596" class="run-this">#include <stdio.h>
#include <string.h>
void replace(char* str, char* r) {
int spaces = 0;
int len = strlen(str);
int i=0;
for(i=0;i<len;i++) {
if(str[i] == ' ') spaces++;
}
if(spaces) {
int p = spaces*(strlen(r)-1);
for(i=len;i>=0;i--)
if(str[i] != ' ') {
str[i+p] = str[i];
} else {
int j = strlen(r)-1;
while(j>=0) {
str[i+p] = r[j];
j--;
p--;
}
p++;
}
}
}

void main() {
char str[100] = "1 2 3 4 5 6 7 8 9 0 10 11 12 ";
// char str[100] = "I am CareerCup ";
printf("%s\n",str);
replace(str,"abc");
printf("%s\n",str);
}
</pre><pre title="CodeMonkey52596" input="yes">
</pre>

- CareerCup September 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Precondition; "str contation more memory..." as in the question.

Space O(1)
speed O(n)where n = strlen(inputstring)-no:of spaces+(no:of spaces * strlen(replacementstring) or just n = final string

- CareerCup September 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Step1: Count no of spaces by parsing the input string - O(n)
Step2: Allocate memory for strlen(input string) + (3-1)*noOfSpaces.
Step3: Copy from input string till we encounter space, replace space with replacementString.
Step4: Continue Step3 till we reach end of string.

- gavinashg September 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

U can't use extra memory..... extra space allocated in the string only....

- Anonymous September 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@FirstSolution your solution is fine but instead of copying to last and start from begining, how about starting from last and do your approach? This avoids initial copying operation

- SecretAgent September 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes..... thnx for ur suggestion

- Anonymous September 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

is this a language specific question?
there is no way you can do this to a string in Java without creating a new string

- nk September 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Like in C/C++, you cant know the exact end of string as u dont use /0

- Anonymous September 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi. How about this one. This does it inplace. I tested it with large samples of data. Simple I think.

# include "stdio.h"
# include "string.h"

int main()
{
char *str = malloc(100*sizeof(char));
int i,len,incnum = 0;
memcpy(str,"Chi laka paka pika boomba ",50);
printf("\n%s\n",str);
len = strlen(str);
for(i = 0 ; i < (len); i++ )
{
if(str[i+ incnum] == ' ')
{
memcpy(str+i+incnum+2,str+i+incnum,len-i);
memcpy(str+i+incnum,"ABC",3);
incnum += 3;
}
}
printf("\n%s\n",str);
free(str);
return 0;
}

- Daya October 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey45515" class="run-this">void ReplaceString(char*& str)
{
int i = 0;
int k = 0;
int j = 0;
int spaces = 0;
while(i != strlen(str))
{
if(str[i] == ' ')
{
spaces++;
i++;
}
else
{
i++;
}
}
char* string_new = (char*)malloc(100*sizeof(char));
memcpy(string_new, str, 50);
memset((char*)string_new + i,'0', 2*spaces + i);
string_new[2*i + 2*spaces] = NULL;
while(j != i)
{
string_new[i + spaces*2 + j] = string_new[j];
j++;
}
k = i + 2*spaces;
int inc = 0;
while(string_new[k] != NULL)
{
int p = 0;
int q = 0;
if(string_new[k] == ' ')
{
string_new[inc] = 'A';
string_new[inc + 1] = 'B';
string_new[inc + 2] = 'C';
inc = inc + 3;
q = inc;
p = k + 1;
while(string_new[p] != NULL)
{
string_new[q] = string_new[p];
q++;
p++;
}
k++;
}
else
{
k++;
inc++;
}
}
string_new[i + 2*spaces] = NULL;
j = 0;
while(string_new[j] != NULL)
{
printf("%c", string_new[j]);
j++;
}
}

</pre><pre title="CodeMonkey45515" input="yes">
</pre>

- Anonymous October 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;

public class textSpace {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		String s = scan.nextLine();
		char[] nip = s.toCharArray();
		char[] ip = new char[nip.length*2];
		int last = nip.length-1;
		int m;
		for ( m =0 ;m<nip.length;m++)
		{
			ip[m]=nip[m];
		}
		char[] a = {'A','B','C'};
		
		
		int i,j,k,n_last=0;
		for (i=last; i>0;i--)
		{
			if(ip[i] == ' ')
			{
				j=last;
				last=last+a.length-1;
				for (;j>i;j--)
				{
					n_last=j+a.length-1;
					ip[n_last]=ip[j];
					
				}
				
				k=i;
				for (int l=0;l<a.length;l++)
				{
					ip[k]=a[l];
					k++;
				}
			}
		}
		for (m =0 ;m<ip.length;m++)
		{
			System.out.print(ip[m]+" ");
		}
		
	}

}

- guru November 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void replace(char str[]);
int main()
{
    char str[100]="Interview in Seattle";
    

    
    printf("%s",str);

    replace(str);
    printf("\n%s",str);    
    getch();
    
}

void replace(char str[])
{
     int space=0,i;

     
     for(i=0;i<strlen(str);i++)
     {
                               if(str[i]==' ')
                               {
                                            memcpy(str+i+3,str+i+1,strlen(str)-i);
                                            memcpy(str+i,"ABC",3);
                               }
     }
                               
                               
                               
}

- Anonymous December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *replaceWhiteSpace (char *s) {
int i;
char *strEnd;
int len = strlen (s)-1;

strEnd = s + len;

while (strEnd != s) {
if (*strEnd == ' ') {

len = strlen((strEnd+1));
char *tmp = strEnd+1;

while (len) {
tmp[len-1+2] = tmp[len-1];
len--;
}
tmp = strEnd;
*tmp++ = 'A';
*tmp++ = 'B';
*tmp++ = 'C';
}
strEnd--;
}
}

- AwesomeSolution April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{I dont know y people r using such abig code whie salesforce provide workflow rules
to repalce....SUBSTITUTE(text, old_text, new_text)
in ex put the text what u want to replace " "(space)
old text :put the text
new put "abc"
short n simple thats it...}

- Abhijeet kankani(abhigtkankani@gmail.com) May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hello mr ,it is interview question by microsoft not viva question by your adhocs......

- Anonymous September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hello dear, what ever company had asked the question its simple method. If salesforce provide us functionality then y notwe r not going for code re-usablity.

- abhijeet kankani December 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>

int main()
{
char ch;
int i=0,k=0;
char *a=malloc(200);
printf("enter the string\n");
while((ch=getchar())!='\n')
a[i++]=ch;
a[i]='\0';
while(k<i)
{
if(a[k]==' ')
{
memmove(a+k+3,a+k+1,i-k-1);
a[k++]='A';
a[k++]='B';
a[k++]='C';
i+=2;
}
else
k++;
}
printf("%s",a);
return;
}

- prabal July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringSplit {
public static void main(String[] args) {
String append = "I am Chandu";
String[] splits = append.split(" ");
append="";
System.out.println("splits.size: " + splits.length);
for(int i=0; i<splits.length;i++){
append=append+splits[i]+"abc";}
System.out.println(append); }}

- Anil January 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void stringReplace(char[] input, char[] replace, char[] output) {
for (int i = input.length - 1, j = output.length - 1; i >= 0; i--, j--) {
if (input[i] != ' ') {
output[j] = input[i];
} else {
for (int k = replace.length - 1; k >= 0; k--) {
output[j] = replace[k];
j--;
}
j++;
}

}
System.out.println(output);
}

- santosh.b March 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String replaceSpace(String source, String replacer) {

        for (int i=0; i<source.length(); i++) {
            if (source.charAt(i)==' ') {
                source = source.substring(0,i)+replacer+source.substring(i+1);
                i+=replacer.length()-1; //Minus 1 since it will add again on the next loop
            }
        }
        return source;
    }

- george.maggessy April 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static char[] ReplaceWithOutMemory(char []str, int length, string replacement, char c)
        {
            int j = str.Length - 1;
            for (int i = length - 1; i >= 0; --i)
            {
                if (str[i] != c)
                {
                    str[j--] = str[i];
                }
                else
                {
                    for (int k = replacement.Length - 1; k >= 0; k--)
                        str[j--] = replacement[k];                        
                }
            }

            return str;
        }

- ia May 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReplaceSpace {

	public static void main(String args[]) {

		ReplaceSpace rs = new ReplaceSpace();
		rs.replace("This is a test", "ABC");
	}

	public void replace(String a, String b) {

		List<Character> newString = new ArrayList<Character>();
		char[] orignal = a.toCharArray();
		char[] replace = b.toCharArray();

		for (int i = 0; i < orignal.length; i++) {
				if (orignal[i] == ' ') {
					for (int k = 0; k < replace.length; k++) {
						newString.add(replace[k]);
					}
				}
				else
					newString.add(orignal[i]);
			}
		
		System.out.println(newString.toString());
	}
}

- d.rijhwani September 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var str = "i am chandu";

str = str.replace(/\s/g, "ABC");

console.log(str); // iABCamABCchandu

- Anbu June 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
// TODO code application logic here

String str="i am chandu";
str=str.replaceAll(" ", "ABC");
System.out.println(str);
}

- Anonymous September 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Example2 {

/**
* @param args
*/

public String fillSpace(String input){

String output= "";
for( int i=0; i < input.length(); i++){
char checkSpace = input.charAt(i);
if( checkSpace == ' '){
output += "ABC";
}else{
output += checkSpace;
}
}
return output;
}
//How to replace the space in the string with "ABC" without using extra memory.. string may contain some extra memory.
//str = "i am chandu" -- str contation more memory...
// str = "iABCamABCchandu"
public static void main(String[] args) {
// TODO Auto-generated method stub
Example2 example2 = new Example2();
System.out.println("Ouput ::::"+example2.fillSpace("I am Santhosh"));
}

}

- Santhosh November 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReplaceSpace {
public static void replace(String str)
{ String a="";
for(int i=0;i<str.length();i++)
{ if(str.charAt(i)==' ')
{ String b="hi";
a+=b; }
else a+=str.charAt(i);
}
System.out.println("the manipulated string is :" +a);
}
public static void main(String[] args)
{
replace("this a great day");
}
}

- Aashish Mahajan March 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void replaceSpaceWith()
{
	int maxlen;
	cout<<"Enter max string length";
	cin>>maxlen;

	//allocate space of len*3, in worst case there will be len spaces
	int max = maxlen*3;
	char str[max];

	cin.ignore();
	cout<<"\n ENter string";
	cin.getline(str,sizeof(str));

	int len = strlen(str);


	int numspaces = 0;
	//find number of spaces
	for(int i = len-1;i>=0;i--)
	{
		if(str[i] == ' ')
		{
			numspaces++;
		}
	}


	int newStrLen = len + (2*numspaces);

	str[newStrLen+1] = '\0';
	for(int i = (len-1), j = (newStrLen-1); i>=0;i--,j--)
	{

		if(str[i] == ' ')
		{

			str[j] = '0';
			str[--j] = '2';
			str[--j] = '%';
		}
		else
		{
			str[j] = str[i];
		}
	}

	cout<< "\n new string is "<<str;
}

- Anonymous September 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void replaceSpaceWith()
{
	int maxlen;
	cout<<"Enter max string length";
	cin>>maxlen;

	//allocate space of len*3, in worst case there will be len spaces
	int max = maxlen*3;
	char str[max];

	cin.ignore();
	cout<<"\n ENter string";
	cin.getline(str,sizeof(str));

	int len = strlen(str);


	int numspaces = 0;
	//find number of spaces
	for(int i = len-1;i>=0;i--)
	{
		if(str[i] == ' ')
		{
			numspaces++;
		}
	}


	int newStrLen = len + (2*numspaces);

	str[newStrLen+1] = '\0';
	for(int i = (len-1), j = (newStrLen-1); i>=0;i--,j--)
	{

		if(str[i] == ' ')
		{

			str[j] = '0';
			str[--j] = '2';
			str[--j] = '%';
		}
		else
		{
			str[j] = str[i];
		}
	}

	cout<< "\n new string is "<<str;
}

- Nishi September 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void replaceSpaceWith()
{
int maxlen;
cout<<"Enter max string length";
cin>>maxlen;

//allocate space of len*3, in worst case there will be len spaces
int max = maxlen*3;
char str[max];

cin.ignore();
cout<<"\n ENter string";
cin.getline(str,sizeof(str));

int len = strlen(str);


int numspaces = 0;
//find number of spaces
for(int i = len-1;i>=0;i--)
{
if(str[i] == ' ')
{
numspaces++;
}
}


int newStrLen = len + (2*numspaces);

str[newStrLen+1] = '\0';
for(int i = (len-1), j = (newStrLen-1); i>=0;i--,j--)
{

if(str[i] == ' ')
{

str[j] = '0';
str[--j] = '2';
str[--j] = '%';
}
else
{
str[j] = str[i];
}
}

cout<< "\n new string is "<<str;
}

- Nishi September 30, 2015 | 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