Intel Interview Question for Software Engineer / Developers


Country: United States




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

1.start from index 0
2.swap the character @ index 0 with its neighbour(1).
3.continue this till the first character reaches the end of the string.
repeat the above steps for N (N is the number of characters tht needed to be moved)

example:
if bcde is the string with N=2; desired output is debc


steps are as follows,
N=1
bcde
cbde
cdbe
cdeb

N=2
cdeb
dceb
decb
debc

- fullthrottle July 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The best solution here..

- bish November 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<iostream>
void main()
{

std::string s1="abcdef";
int pos=3;
s1.append(s1.substr(0, pos));
s1=s1.substr(pos, s1.length());
std::cout<< s1.c_str();
}

- Shantanu July 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

whats std:: ??

is it the namespace ???

- Asis October 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes.
You can remove the std:
by doing

include namespace std;

after the #includes end

- KaranGoswamiKenZ April 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes.
You can remove the std:
by doing

include namespace std;

after the #includes end

- KaranGoswamiKenZ April 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Work it as a circular queue and shift it.

- VK July 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is indeed the best solution.

- Anonymous January 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is indeed the vaguest solution.

- Anonymous January 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char * shiftString(char* str, int shiftBy)
{
    char *buff = malloc((sizeof(char) * shiftBy)  + 1);
    int i = 0;
    strncpy(buff,str,shiftBy);
    buff[shiftBy] = '\0';
    while(str[i + shiftBy] != '\0')
      str[i] = str[i + shiftBy];
    strcat(str,buf);
    return str;
}

- Ashutosh July 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry i forgot to increment variable 'i' in the loop.
The correct code is:

char * shiftString(char* str, int shiftBy)
{
    char *buff = malloc((sizeof(char) * shiftBy)  + 1);
    int i = 0;
    strncpy(buff,str,shiftBy);
    buff[shiftBy] = '\0';
    while(str[i + shiftBy] != '\0')
      str[i++] = str[i + shiftBy];
    strcat(str,buf);
    return str;
}

- Ashutosh July 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey Ashutosh....your program has a small hitch....when you do the str[i++]=str[i+shiftBy] and concatenate, the array str has two instances of the same string upto the shiftBy index.

For ex:
if input is abcdef and shifBy is 3
then at the end of the while loop str has defdef and then when you concatenate, it returns abcdefdef....

I would recommend the following correction
char * shiftString(char* str, int shiftBy)
{
char *buff = malloc((sizeof(char) * shiftBy) + 1);
int i = 0;
strncpy(buff,str,shiftBy);
buff[shiftBy] = '\0';
while(str[i+shiftBy]!='\0')
{
str[i]=str[i+shiftBy];
str[i+shiftBy]='\0';
i++;
}
strcat(str,buf);
return str;
}

Apart from this your program works fine...thanks...

- Gokul July 21, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

guys ..
above program is not gonna work. Serious flaw is when u strcat() prefix substring to remaining original string. For strcat() the space in target string must be enough to hold the source string and if u add prefix string to this end , it may cause memory overlap.

- mrn July 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

though i have not tried other soution...i like fullthrottle's solution which is simple and easily understandable...very nice..though might not be that efficient...coool soln

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

# include <iostream.h>
# include <conio.h>
# include <string.h>
# include <malloc.h>
int main()
{
char str[10],str2[10];
int pos;
char *s,*t;
t=str2;
cin>>str;
cin>>pos;
s=str;
if(pos>strlen(str))
cout<<"pos reater than length of string";
else
{
while(pos!=0)
{
*t=*s;
s++;
t++;
pos--;
}
*t='\0';
cout<<strcat(s,str2);
}

getch();
}

- Anonymous July 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

conio.h is nonstandard and doesnt look good in most interviews, especially with companies like intel that tend to use unix environments where conio is useless anyways.

- anonymous May 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

was this interiew for fulltime or internship. I am INTERN in INTEL just wanted to know if they had started hiring. As per me they r in hiring freeze

- CUNOMAD July 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i=0;
count=0;
char temp = str[i]


while(count<=length)
{
count++;

if ( i < index)
	newi = length - index + i
else
	newi = i - index

char temp_1 = str[newi];
str[newi] = temp;

i=newi;
temp = temp_1;
}

complexity : O(n)

- shipraverma25 July 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void shift(char str[], int i) {
    if (i <= 0) return; // no op

    int len = strlen(str);

    if (i >= len) return; // no op

    char* tmp = new char[i];

    strncpy(tmp, str, i);

    str[0] = 'c';

    for (int j = i; j < len; ++j) str[j - i] = str[j];

    strncpy(str + len - i, tmp, i);
    delete[] tmp;
    cout << str << endl;
}

- Anonymous July 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about this? I think this will be done in time O(n)

ShiftArray(char *arr, int index) {
        for(int i = index; arr[i] != '\0'; i++) {
             Swap(arr[i-index],arr[i]);
        }
}

- Dinesh July 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect!!!

- Anonymous July 27, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

the above solution won't work for "abcde"

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

int mod(int a,int b)
{        
    if(a<0)
    {
    a=-a;
    return b-(a%b);
    
    }
    
    else
    return a%b;
    
}

string shiftBACK(string text,int index)
{
  int i,sz=text.size();
  string retv(text.begin(),text.end() );
  
  for(i=0;i<text.size();i++)
  retv[mod( (i-index),sz )]=text[i] ;
  
  return retv;
  
  
}

string shiftFRONT(string text,int index)
{
       int i,sz=text.size();
       string retv(sz,'0');
       
       for(i=0;i<text.size();i++)
       retv[(i+index)%sz ]=text[i];
       
       return retv;
       
       }

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

reverse the string once

g f e d c b a

reverse the string from 0 to (len -k -1) and reverse the string from (len-k) to (len -1) index

- Geekenized August 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is one of the problems in programming pearls.Bentley gives a very good solution.

- tj August 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

tj, the solution he gives is the exact one Geekenized gave.

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

char *stringFlip(char *str, int ind){
   
   int i = 0;
   if (strlen(str) < ind -1 )
     return str;

   char *revstr = (char *)malloc(sizeof(char)*(strlen(str)+1));
   while ( *(str+i+ind) != '\0' ){
       *(revstr+i) = *(str+i+ind);      
       i++;
   }
   int j = 0;
   while ( j < ind){
        *(revstr+i+j) = *(str+j);
        j++; 
   }
   *(revstr+i+j) = '\0';
   return revstr;
}

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

I have not seen the code written by others, but I am assuming that most of the solutions are O(n). You can actually do it O(log n) time by using splay trees.

Encode the string which is a dynamic sequence as a splay tree. Building this splay tree will take O(n) time. But you can answer any query of the form shift(k) in O(log n) time once you build this splay tree. Splay trees are very special in the sense that they can o splits and joins. In fact, for the above problem we require only 1 split at position n-k and 1 join at position 1. Both splits and joins take only O(log n) amortized time. So if you do a sequence of k operations, it will run in only O(k log n) in the worst case.

For further details on splay trees, please refer wikipedia or "Introduction to Algorithms" by Cormen et al.

- Catalan May 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void rotate(char str[], int k)
{
int n;

n = strlen(str);

reverse(str, 0, k-1);
reverse(str, k, n-1);
reverse(str, 0, n-1);
}
where,
k = pivot
n = string length

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

void rotate(char str[], int k)
{
int n;

n = strlen(str);

reverse(str, 0, k-1);
reverse(str, k, n-1);
reverse(str, 0, n-1);
}
where,
k = pivot
n = string length

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

Have not read above solutions.But think this would work.

old_src = 'abcdef'
new_src = old_src.concat(old_src) = 'abcdefabcdef'
return new_src.substring(index,index+o_src.length)


example :index 5
would return new_src.substring(5,11) = fabcde

- Ran May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

void main()
{
 char str[]="abcdef";
 int n=strlen(str);
 int index=3,temp,i;

 while(index--)
  {
   temp=str[0];
   for(i=1;i<n;i++)
    { 
     str[i-1]=str[i];
    }
   str[i-1]=temp;
   
  }
 printf("%s",str);

}

- raktim September 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main ()
{
char str[10] = "ankit";
int t;
char* buf;
int i=0;
printf("enter the string \n");
scanf("%s",str);
printf("enter num \n");
scanf("%d",&t);
buf = (char*)malloc(sizeof(char)*t);
strncpy(buf,str,t);
buf[t]='\0';
while(str[i+t]!='\0')
{
str[i]=str[i+t];
i++;
}
str[i]='\0';
strcat(str,buf);
printf("%s",str);
getch();
}

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

#include<iostream>
using namespace std;

string shiftString(string str,int n)
{
    char s[str.length()];
    int l=str.length();
    int i=0;
    s[l]='\0';

    for(i=n;i<l;i++){
        s[i-n]=str[i];
    }

    for(int j=0;j<n;j++){
        s[i-n]=str[j];
        i++;
    }
    return s;
}
int main()
{
    string str="abcdef";
    cout<<shiftString(str,3);
    return 0;
}

- Jignesh July 04, 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