Intel Corporation

  www.intel.com
Work in HR? Unlock Free Profile

Interview Question

Anonymous Interview Santa Clara, CA

If we have a string

of 7 characters and we can put from 'a' to 'z' in any of these places, write a program which its output is from 'aaaaaaa' to 'zzzzzzz'.
Answer

Interview Answer

1 Answer

1

Using recursion. Maybe the first thought in our mind is for loop but after second thought I believe is most suitable for this question. Following is my program using C++.
void Str(int str_size, int index, char *str)
{
    if(str_size == index)
    {
        str[index] = '\0';
        cout<<str<<endl;
        return;
    }
    for(char c = 'a'; c <= 'z'; ++c)
    {
        str[index] = c;
        Str(str_size, index + 1, str);
    }
}

Tishin on Sep 25, 2012

Add Answers or Comments

To comment on this question, Sign In with Facebook or Sign Up