Cisco Systems Interview Question for Software Engineer / Developers


Team: NOSTG
Country: India
Interview Type: In-Person




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

public String replaceString(String str, String str1, String str2){
String[] strArr = str.split(" ");
String Buffer finalString = new StringBuffer();
for(int i=0;i<strArr.length;i++)
{
if(strArr[i].equals(str1) {
if(i==0)
finalString.append(str2);
else{
finalString.append(" ");
finalString.append(str2);
}
}else{
if(i==0)
finalString.append(strArr[i]);
else{
finalString.append(" ");
finalString.append(strArr[i]);
}
}
}
return finalString.toString();
}

- simranbahri1 November 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static String replace(String str, String s1, String s2){
str = str.replaceAll(s1, s2);
return str;
}

- Santosh May 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static String replace(String str, String s1, String s2){
str = str.replaceAll(s1, s2);
return str;
}

- Santosh May 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

void *strrp(char *src,char *sub,char *rp,char *p)
{
    int sub_len=strlen(sub);
    char *po=NULL,*q=src;
    while((po=strstr(q,sub))!=NULL)
    {
        strncat(p,q,po-q);
        strcat(p,rp);
        q+=po-q+sub_len;
    }
    strcat(p,q);
}

- nz2324nz2324 December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will not work. You are operating on the same memory space. The first strcat does not take into account, overwriting main string after the occurence of the repeating pattern, if the replacement pattern is longer than the original pattern

- raj December 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

crackprogramming.blogspot.com/2012/10/replace-all-occurrence-of-in-given.html

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

Should the output be what is stated in the question above

OR

// replace s1 with s2 in place and expand original string
"Hi i am pqrstuv and i am in pqrstuv"

OR

// best effort replacement of s2, without expanding original string
"Hi i am pqrstuv i am in pqr"

- Anonymous November 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ohh i am so sorry its a typo!!..

final string shud be "Hi i am pqrstuv and i am in pqrstuv"

- jayeshr007 November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We have make sure the following :
1. 'str' is big enough to accommodate replace string, as the replace string can be bigger than the original string.
2. 'str' is not a constant.

int findAndReplace(char *str,int strSize,char *find,char *replace)
{
char *found;
int lenStr = strlen(str);
int lenFind = strlen(find);
int lenReplace = strlen(replace);
char *endStr = str + lenStr;
char *endStrMax = str + strSize;
int lenDiff;
int ret = 0;

while(found = strstr(str,find))
{
if (lenFind == lenReplace)
{
memcpy(found,replace,lenFind);
}
else if (lenFind > lenReplace)
{
memcpy(found,replace,lenReplace);
memcpy(found+lenReplace,found+lenFind, endStr - found - lenFind );
}
else if (lenFind < lenReplace)
{
lenDiff = lenReplace - lenFind;
if (lenStr + lenDiff > strSize)
{
ret = -1; //out of bounds
}
else
{
memcpy(found+lenReplace,found+lenFind,endStr - found - lenFind);
memcpy(found,replace,lenReplace);
lenStr += lenDiff;
endStr += lenDiff;
}
}
}
return ret;
}

- Pranav November 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Something similar here?



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

int main(int argc, char* argv[])
{
int i;

char arr[100];
char* a;
char* b="abc";
char* c="pqrstuv";
int lenb=strlen(b);
int lenc=strlen(c);
char* ptrb=malloc(lenb);
char* out;
char* outtemp;

fgets(arr,100,stdin);
a=arr;
out=(char*)malloc(5*strlen(a));
outtemp=out;

memset (out,0,strlen(a)+1);
memset (ptrb,0,lenb);

for (i=0;*a;i++)
{
// Copy lenb characters into temporary array
strncpy(ptrb,a,lenb);

// Compare temporary array with reference b
if (!strcmp(ptrb,b))
{
// If match, replace with other string.
strncpy(out,c,lenc);
out+=lenc;
a+=lenb;
}
else
{
// Increment pointers
*out++=*a++;
}
}

// Print output
*out='\0';
fputs(outtemp,stdout);

// Free temporary memory
free(outtemp);
}

- Srujan December 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

implemented in C, with o(n) space and ~o(n) time complex.

char * replaceStr(char *sOriginal, char *sSub, char *sNew)
{
int olen = strlen(sOriginal);
int subLen = strlen(sSub);
int newSubLen = strlen(sNew);
int i, j, k=0;
char *sOutPut = NULL;
sOutPut = (char *)malloc(100);
memset(sOutPut, 0, 100);
memcpy(sOutPut, sOriginal, olen);
printf("%s\n\r", sOutPut);

for(i=0; i<100 && sOutPut[i]!='\0'; i++)
{
for(j=0; j<subLen; j++)
{
if(sOutPut[i+j] == sSub[j])
{

}else
{
break;
}
}

if(j==subLen)
{
//find a substring, then replace sSub with sNew
memcpy(&sOutPut[i+newSubLen], &sOriginal[k+subLen], olen-k-subLen+1);
memcpy(&sOutPut[i], sNew, newSubLen);
i = i+newSubLen-1;
k = k+subLen;
}else
{
k++;
}
}

printf("%s\n\r", sOutPut);

}

- Bin December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oops, I forgot head/tail word.
if(j==subLen)
{
if((i==0 && (sOutPut[i+subLen]==' ')) || (i!=0 && (sOutPut[i-1]==' ')))
{

- Bin December 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {

String str = "I am abc. I am in abc";
System.out.println(str);
String s1 = "abc";
String s2 = "tcs";
str= str.replaceAll(s1, s2);
System.out.println(str);
}

- Arya February 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<?php $bodytag = str_replace("abc", "pqrstuv", "Hi i am abc and i am in abc");?>

- hai php February 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <cstring>
using namespace std;

void replace(char *s, char *s1, char *s2, char *d){
char *r = strstr(s, s1);
strncpy(d, s, r-s);
strcat(d, s2);
while(*r != ' ' && *r != '\0') r++;
strcat(d, r);
}

int main(){
char *s = "Hello this damn World!";
char *s1 = "damn";
char *s2 = "nice";
char *d = new char[80];
replace(s, s1, s2, d);
cout<<s<<endl;
cout<<d<<endl;
}

- Leo October 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

works only for first occurrence. Fails for this.

#include <iostream>
#include <cstring>
using namespace std;

void replace(char *s, char *s1, char *s2, char *d){
char *r = strstr(s, s1);
strncpy(d, s, r-s);
strcat(d, s2);
while(*r != ' ' && *r != '\0') r++;
strcat(d, r);
}

int main(){
char *s = "Hello this damn World! damn";
char *s1 = "damn";
char *s2 = "nice";
char *d = new char[80];
replace(s, s1, s2, d);
cout<<s<<endl;
cout<<d<<endl;
}


OP:
Hello this damn World! damn
Hello this nice World! damn

it should be :
Hello this damn World! damn
Hello this nice World! nice

- Anand April 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void strrp(char *src,char *sub,char *rp,char *p)
{
   int delta ;
   int sizesub=strlen(sub);
   int sizesrc=strlen(src);
   char* q;
   while(q=strstr(src,sub))
   {
     strncat(p,src,q-src);
	 strcat(p,rp);
     delta=q-src;
	 src=src+sizesub+delta;
   }
   strcat(p,src);
}

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

Cisco gives hike once in 2-3 years.

New-Joinees only get hike after 2-3 years, so take 100% hike at the time of joining .. . otherwise don't cry after joining. :)

- Simple April 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package string_replace;

import java.util.StringTokenizer;

public class Strrep {
	static String strrep (String str, String s1, String s2) {
		StringBuilder sb = new StringBuilder();
		StringTokenizer st = new StringTokenizer(str, " ");
		while (st.hasMoreTokens()) {
			String word =st.nextToken();
			if (word.equals(s1)) {
				word=s2;
			}
			sb.append(word);
			sb.append(" ");
		}
		return sb.toString();
		
	}

	public static void main (String[] args) {
		String str=args[0];
		String s1=args[1];
		String s2=args[2];
		System.out.println("Original string is "+str);
		String str2=strrep(str,s1,s2);
		System.out.println("New string is "+str2);
		
	}
}

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

void replace(char *s, char *find, char *replace)
{
char * pos = strstr(s, find);
if(pos == NULL) return;
char tmp[20];
strcpy(tmp, pos + strlen(find));
strcpy(pos, replace);
strcat(s, tmp);
}

- bacho April 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import re
def replace_string(sout,s1,s2):
print("Before replacing: %s " %sout)
new = re.sub(s1,s2,sout)
print("After replacing : %s" %new)

if __name__=='__main__':
str = "hi am abc, i live in abc"
s1 = "abc"
s2 = "sbp"
replace_string(str,s1,s2)

- sbp November 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Use the Knuth algorithm to find the occurrence(s) of the s1 in str1 and replace every such occurrence once found. Knuth algorithm has a complexity of O(strlen(str1)) plus a O(strlen(s1)) pre-proccessing. Replacing will take maximum of O(strlen(s1)).

- Anonymous November 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This string clearly is divided into seperate words...KMP algo is not required here.

- abhinav.neela November 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public String findBigStringAndReplace(String str, String str2) {
        StringTokenizer st = new StringTokenizer(str, ".");
        int bigLength = 0;
        String bigWord = "";
        while (st.hasMoreTokens()) {
            String word = st.nextToken();
            int wordLength = word.length();
            if (word.length() > bigLength) {
                bigLength = wordLength;
                bigWord = word;
            }
        }
        return str.replaceAll(bigWord, str2);

}}

- Anonymous November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public String findBigStringAndReplace(String str, String str2) {
        StringTokenizer st = new StringTokenizer(str, " ");
        int bigLength = 0;
        String bigWord = "";
        while (st.hasMoreTokens()) {
            String word = st.nextToken();
            int wordLength = word.length();
            if (word.length() > bigLength) {
                bigLength = wordLength;
                bigWord = word;
            }
        }
        return str.replaceAll(bigWord, str2);

}
// Pavan Kumar Naidu

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

Using String.split() is better then using a StringTokeniser. String.split uses regex matcher so it can do the job quickly.

also there would be better solution for the above question. this was just to correct the above comment

- Punith November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

char *replace_str(char *str, char *orig, char *rep)
{
static char buffer[4096];
char *p;

if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
return str;

strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
buffer[p-str] = '\0';

sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

return buffer;
}

int main(void)
{
puts(replace_str("Hello, world!", "world", "Miami"));

return 0;
}

- balajiforwin@gmail.com November 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is only replacing the first occurrence of the orig.. not all

Hi i am rahul and i am in abc

- jainrajrahul December 04, 2012 | Flag


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