Interview Questions

Given an array containing lower case and upper case alphabets

Software QA/Tests Interview Questions from Microsoft


(Continued from previous question...)

Given an array containing lower case and upper case alphabets

Question:
Software Engineer in Test:
Given an array containing lower case and upper case alphabets and numbers, how can you sort/arrange the array in one single pass using just one variable for swapping such that the resultant array should put the input elements into 3 buckets in the following fashion -
Input - aA1B23Cbc4
Output - abcABC1234

Note - ordering doesn't matter
the output could be -
ABC1234abc or 1234abcABC

You just have to arrange the data into 3 buckets in single pass using just one temp variable for swapping. Expected runtime - o(n)


maybe an answer:


int l = a.length();
int s = 0;
int e = l-1;
int i =0;
while(i<l){
if('A' <= a[i] && a[i] <= 'Z' && i>s){
swap(a[i], a[s]);
s++;
}else if('a' <= a[i] && a[i] <= 'z' and i<e){
swap(a[i], a[e]);
e--;
}else i++; }

(Continued on next question...)

Other Interview Questions