Did-it.com Interview Question for Software Engineer / Developers






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

This seems easy. Have a second string or a char array. Read the given string char by char & copy every char to the new string. Whenever we encounter 'A' or 'a' in the given string, copy the char 'o', 'n' & 'e' into the new string at the current index. We will obviously need 2 indices, one to parse the given string and another to form the new string.

Eg. "This is a bus" becomes "This is one bus".

This process just takes one loop. Hence, O(n) is the complexity.

- Vik October 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This seems easy. Have a second string or a char array. Read the given string char by char & copy every char to the new string. Whenever we encounter 'A' or 'a' in the given string, copy the char 'o', 'n' & 'e' into the new string at the current index. We will obviously need 2 indices, one to parse the given string and another to form the new string.

Eg. "This is a bus" becomes "This is one bus".

This process just takes one loop. Hence, O(n) is the complexity.

- Vik October 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Code:

public static String replace(String oldStr)
	{
		String replacedRst="";
		char curChar;
		for(int i=0; i<oldStr.length(); i++)
		{
			curChar=oldStr.charAt(i);
			if((curChar=='a')||(curChar=='A'))
			{
				replacedRst=replacedRst+"one";
			}
			else 
				replacedRst=replacedRst+curChar;
		}
		return replacedRst;
	}

- creation October 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
Piss off
}}

- vin November 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

u can't use inbuilt fn !!!!!!!!!11

- Anonymous November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char a[25], b[35];
int i,j;
clrscr();
printf("\nEnter string: ");
scanf("%s",&a);
j=0;
for(i=0;i<strlen(a);i++)
{
if(a[i]=='A' || a[i]=='a')
{
b[j]='o';
b[j+1]='n';
b[j+2]='e';
j=j+3;
}
else
{
b[j]=a[i];
j=j+1;
}
}
printf("\nNew string is: ");
for(i=0;i<strlen(b);i++)
printf("%c",b[i]);
getch();
}

- Anonymous November 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C# Code


using System;
using System.Collections.Generic;
using System.Text;

namespace ReplaceString
{
class Program
{
static void Main(string[] args)
{

string stringinput = Console.ReadLine();
string stringReplaced = "";
for (int i = 0; i < stringinput.Length; i++)
{
if (stringinput[i] != 'a' && stringinput[i] != 'A')
stringReplaced += stringinput[i].ToString();
else
stringReplaced += "one";

}
Console.WriteLine(stringReplaced);
Console.ReadLine();



}
}
}

- Hao December 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Should not replace 'A' or 'a' with 'one', but replace 'A ' with 'One ' and ' a ' with ' one '.

- kulang December 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Also, replace only if 'A' or 'a' are words by themselves. Thus "A man and a woman" becomes "One man and one woman"

- Andy December 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

step1 : words[] = sentence.split(" ") // splitting sentence on every space
step2: foreach word in words[], check if it is "A" or "a", word = "One"
Step3: StringBuilder replacedSentence = replacedSentence.append(word+" ");

Complexity overview: Extra memory complexity in terms of String array, builder usage, but O(n) in solving the problem

Any Comments ?

- rajat December 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another approch which can somewhat reduce memory complexity is using a stringTokenizer as follows:


public String replaceAllWords() {
String original = "A man and a woman";
String result = "";
String delimiters = " ";// space
StringTokenizer st = new StringTokenizer(original, delimiters, true);
while (st.hasMoreTokens()) {
String w = st.nextToken();
if (w.equals("a") ||w.equals("A")) {
result = result + "One";
} else {
result = result + w;
}
}
return result;
}

- rajat December 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string.h>
#include <iomanip>

using namespace std;
int main()
{
int n,i;
string str;
cin>>str;
n=str.length();
cout<<n<<endl;
/*char *a=new char[n+1];
strcpy(a,str.c_str());

for (i=0;i<n;i++)
{
if (a[i]=='a'||a[i]=='A')
{
a[i]='1';
}
}*/

for(i=0; i<=n; i++)
{
if(str.at(i) == 'a')
str.at(i) = '1';
}
cout<<str<<endl;

return 0;



}

- dnyaneshwari December 28, 2009 | 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