Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8358211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:43:28+00:00 2026-06-09T10:43:28+00:00

I recently got asked this during an interview. As a recent graduate, and only

  • 0

I recently got asked this during an interview. As a recent graduate, and only been programming about 2 years (all school work), I was at a loss. I had a vague idea, but I’m sure I failed it. This is what I’d written:

string Reverse(string word, string reversed)
{
    if(word.length() == 0)
    {
        return reversed;
    }
    else
    {
        string temp;
        reversed = word.substr(0,1) + reversed;
        temp = word.substr(1);
        Reverse(temp, reversed);
    }

    return reversed;
}

Now that I’m home, I’m testing it, and the return is only the first letter in the input. I’m vaguely familiar with the concept of recursion, but I’m obviously failing at this. Any help/pointers/suggestions are very much appreciated. Thank you.

EDIT:
Following Dennis Meng’s post, I’ve made the following change:

string Reverse(string word, string reversed)
{
    if(word.length() == 0)
    {
        return reversed;
    }
    else
    {
        string temp;
        reversed = word.substr(0,1) + reversed;
        temp = word.substr(1);
        return Reverse(temp, reversed);
    }
}

Now, I get the proper return value. Thank you so very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:43:30+00:00Added an answer on June 9, 2026 at 10:43 am

    What’s going wrong is in here:

        else
        {
            string temp;
            reversed = word.substr(0,1) + reversed;
            temp = word.substr(1);
            Reverse(temp, reversed); // <-- Here's your problem
        }
    
        return reversed;
    }
    

    You know that the call should return the correct answer, so why not just return that? In other words, what if you did

        else
        {
            string temp;
            reversed = word.substr(0,1) + reversed;
            temp = word.substr(1);
            return Reverse(temp, reversed);
        }
    }
    

    instead? The specifics of why your code was only returning the first letter involves pass-by-reference/pass-by-value; because of the pass-by-value stuff, you never actually used what was done in the recursive calls. (You just made the call and threw away what it returned.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently asked just about the same question, but this one got a little
I recently got a shiny new development workstation. The only disadvantage of this is
I recently asked this question here and got some great answers! Custom SQL GROUP
I recently asked this question and got back a great solution using jquery for
i recently asked this question and i got a lot of great answers breaking
I recently asked this question https://softwareengineering.stackexchange.com/questions/129076/go-instead-of-c-c-with-cgo and got some very interesting input. However there's
I recently asked about accessing data from SPSS and got some absolutely wonderful help
I recently asked this question on here and got the answer. However I'm now
I recently asked this question in the pyglet-users group, but got response, so I'm
I recently asked a question about Self-Joins and I got a great answer. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.