Adobe Interview Question
Developer Program EngineersCountry: India
Interview Type: In-Person
At max (3/2)n operations are performed.
It may stop while performing in-place operations, so for then the outer while (1) loop is there.
It will stop when the alphabets are placed completely. It means that all elements are in proper position!
void changeStringa1b2c3d4Toabcd1234(char str[], int size) {
int mid = size>>1 ;
int inext = mid - 1, restartIndex = 1;
char ctemp = str[inext];
while (1) {
do {
if ( ctemp >= 'a' && ctemp <= 'z' )
inext = (ctemp - 'a') ;
else inext = (ctemp-'1')+(size>>1) ;
swap (&ctemp, &str[inext]);
} while ( ctemp != str[inext] ) ;
while ( (restartIndex < mid) && str[restartIndex] == ('a'+restartIndex) ) {
restartIndex ++ ;
}
if ( restartIndex < mid ) {
ctemp = str[restartIndex] ;
} else break ;
}
}
main()
{
char a[10],a1[10],a2[10];
a="s1d3b2m0";
int j=0,k=0;
for(int i=0;i<strlen(a);i++)
{
if(a[i]>='a' && a[i]<='z')
{a1[j]=a[i];j++;}
else
{a2[k]=a[i];k++;}
strcpy(a,a1);
strcat(a,a2);
}
}
Use the partition from quick sort.
Take two index. one at start and one at the end.
i=0, j=n;
while(i <j )
{
while(isAlpha(a[i++]);
while(isdigit(a[j--]);
swap(a[i], a[j]);
}
Handle Boundary Conditions and cross overs
byte[] inputAscii = Encoding.ASCII.GetBytes("s1d3b2m0");
string output = string.Empty;
for (int i = 0; i < inputAscii.Length; i++)
{
if (inputAscii[i] >= 48 && inputAscii[i] <= 57)
{
output = output + (char)inputAscii[i];
}
else if (inputAscii[i] > 96 && inputAscii[i] < 123)
{
Console.Write((char)inputAscii[i]);
}
}
Console.WriteLine(output);
private static void InplaceSort()
{
byte[] inputAscii = Encoding.ASCII.GetBytes("s1d3b2m0");
string output = string.Empty;
for (int i = 0; i < inputAscii.Length; i++)
{
if (inputAscii[i] >= 48 && inputAscii[i] <= 57)
{
output = output + (char)inputAscii[i];
}
else if (inputAscii[i] > 96 && inputAscii[i] < 123)
{
Console.Write((char)inputAscii[i]);
}
}
Console.WriteLine(output);
}
public static String sort(String a) {
char[] characters = a.toCharArray();
int start = 0;
for (int i = 0; i < characters.length; i++) {
if(characters[i] >= 'a' && characters[i] <= 'z') {
int k = i;
while(k > start) {
char temp = characters[k];
characters[k] = characters[k - 1];
characters[k - 1] = temp;
k--;
}
start++;
}
}
return new String(characters);
}
Step 1 : Count no.of Chars.
Step 2 : Start a pointer "i"from 0 and "j" from count+1.
Step 3: Increment i until you find a number or until count and j until you find a char or until end.
Step 4: Just swap the number and char and repeat step 3.
char * GivenPatern( char *s)
{
int temp= 0, l=0;
{
while( l<n)
{
if(iSchar(a[l]) && isdigit(a[l+1]))
{
temp = l;
l++;
}
else if( (isdigit(a[l]) && isdigit(a[l+1])) || ((iSchar(a[l]) && iSchar(a[l+1])) )
{
l++;
}
else if(isdigit(a[l]) && iSchar(a[l+1]))
{
swap( a[l], a[l+1]);
l =temp;
l++;
}
if( m == sizeof(s)/2)
{
return s;
}
}
}
Start from second element(since 1st is at its right place). note its index and character. place that character at right position(according to the final string) and hold the character and index replaced by it. Then place this character again at its proper position. There happens one situation that next index comes to the starting index. Since you have processed this index, you should set it to some unprocessed index. I set it to
strlen(str)/2 + 1
.
(It's my first post at the forum. Do comment if you like it :D)
#include<stdio.h>
#define swap(a, b) {(a)^=(b)^=(a)^=b;}
int main()
{
char str[50];
int n, next_index, lenby2;
char next_char;
sprintf(str, "a1a2b1b2");//abcdef123456
n = strlen(str);
lenby2 = n / 2;
printf("\ninput: %s", str);
for(next_index = 1, next_char = str[1]; n - 2; n--)
{
printf("\n %s %d %c", str, next_index, next_char);
if(next_char >= '0' && next_char <= '9')
{
next_index = lenby2 + next_index / 2;
swap(next_char, str[next_index])
}
else
{
next_index = next_index / 2;
swap(next_char, str[next_index])
}
if(next_index == 1)
{
next_index = lenby2 + 1;
next_char = str[next_index];
}
}
printf("\noutput: %s", str);
getch();
return 0;
}
Use double pointers pointing to the first and the last element respectively.
The first pointer moves to right until it finds a number, while the right pointer moves to left until it finds a character.
swap the number and the character
iterate the two steps until the two pointers meet.
thats no prob....i think...
we can do this by using two index variables......
one pointing to the next element to scan & other point to next position to store that element...
if it is a char.... store it at the same instance........else if it is a number... store it in temp variable using temp=temp*10+element-'0'.
atlast instead of printing.... store it on array.
int isno(char c);
- akiitbhu August 20, 2012void main()
{
char s[]="gs1t43fd86gd2b3m0",t; ///suppose we got
int i=0,j=0,k;
while(i<strlen(s))
{
if(isno(s[i]))
i++;
else
{
t=s[j];
s[j]=s[i];
if(i>j+1)
{
k=i;
while(k>j)
{
s[k]=s[k-1];
k--;
}s[j+1]=t;
}else
s[i]=t;
i++;j++;
}
}
printf("%s",s);
}
int isno(char c)
{
return (c<='9'&&c>='0')?1:0;
}
int ischar(char c)
{
return ((c<='z'&&c>='a')||(c<='Z'&&c>='A'))?1:0;
}