Amazon Interview Solution: Replace all 0's with 5


Given a number N. The task is to complete the function convertFive() which replace all zeros in the number with 5 and returns the number.
Input:
The first line of input contains an integer T, denoting the number of test
cases. Then T testcases follow. Each testcase contains a single integer N denoting the number.
Output:
The function will return integer where all zero's are converted to
5.
User Task:
Since this is a functional problem you don't have to worry about input, you just have to complete the function
convertFive().
Constraints:
1 <= T <= 103
1 <= N <= 104

Example:
Input

2
1004
121

Output
1554
121



Solution

int convertFive(int n)
{
   //Your code here
    int i,j,k,l,sum;
    vector<int>v;
    l=n;
    sum=0;
    while(n!=0)
    {
        k=n%10;
        if(k==0)
        {
            v.push_back(5);
        }
        else
        {
            v.push_back(k);
        }
        n=n/10;
    }
    if(l==0)
    {
        return l;
    }
    else
    {
        for(i=v.size()-1;i>=0;i--)
        {
            sum=sum*10+v[i];
        }
       
        return sum;
    }
   
   
     
}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment