N/A Interview Question for xyzs


Country: United States
Interview Type: Written Test




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

I was given this one last week as well. Only took a couple minutes to write a base version but accidentally passed a char* through the function (which doesn't work) instead of the 'char astring[]' mentioned in the example lol. Here is the code I submitted and what I wanted it to be.

Turned in:

void translate(char* str)
{
  if(!str) return;
  int size - 0;
  while(*str)
  {
    size++;
    str++;
  }
  str -= size;
  for(int i = 0; i < size; i++)
  {
    if(str[i] == 'A' && str[i + 1] == 'B')
    {
      str[i] = 'C';
      int j = i + 1;
      do
        str[j] = str[j + 1];
      while(++j < size);
  }
}

what it would have ended up as...

void translate(char* str)
{
  if(!str) return;
  for(; *str; str++)
  {
    if(*str == 'A' && *str + 1 == 'B')
    {
      *str = 'C';
      int L = 1; // "increment" str mathematically
      for(char* c = str + 1; *c; c++, L++)
        *(str + L) = *(c + 1);
    }
  }
}

Like I said, I passed a char pointer instead of an array when testing it. By the time I figured that out, I didn't have enough time to make the modifications. Downside to a two step programming process I guess. Bummer.

- gscottiv December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the solution with O(n) complexity

void translate(char* str){
     
    int i=0,c=0;
    while(str[i] != '\0'){
        if(str[i] == 'A' && str[i+1]=='B'){
            str[c]='C';
            i=i+2;
            c++;
        }else{
            str[c++] = str[i++];
        }
    }
    str[c]='\0';
}

- HJK December 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
void translate(char astring[40])
{
	int i=0;
	int k,j;
	k=strlen(astring);
	while(astring[i]!='\0')
	{
		if(astring[i]=='A'&&astring[i+1]=='B')
		{
			astring[i]='C';
			j=i;
			while(j<k)
			{
				astring[j+1]=astring[j+2];
				j++;
			}
		}
		i++;
    }
}
int main()
{
	char astring[40];
	printf("enter string");
	gets(astring);
	printf("original string was:%s\n",astring);
	translate(astring);
	printf("string is %s",astring);
	return 0;
}

- apoorva December 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In python

def ConvertString(s, t, w):
    new = ''
    i = 0
    while i < len(s):
        if s[i] == t[0]:
            if s[i:i+len(t)] == t:
                new += w
                i += len(t)
            else:
                new += s[i]
                i += 1
        else:
            new += s[i]
            i += 1
    return new

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

#include <stdio.h>
#include <stdlib.h>
void translate(char*);

int main()
{
    char str[20];
    printf("enter any string");
    fgets(str, 20, stdin);
    translate(str);
    printf("\nthe translated string is \n");
    printf("\n%s", str);
}

void translate(char* str)
{
    if(str[0]=='\0')
        return;
    int i=1, j;
    while(str[i]!='\0')
    {
        if(str[i-1]=='A'&&str[i]=='B')
        {
            str[i-1]='C';
            for(j=i;str[j]!='\0'; j++)
            {
                str[j]=str[j+1];
            }
            i--;
        }
        i++;
    }
    return;
}

- Sandeep Singh December 27, 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