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 253017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:43:35+00:00 2026-05-11T21:43:35+00:00

I had an interview question that asked me for my ‘feedback’ on a piece

  • 0

I had an interview question that asked me for my ‘feedback’ on a piece of code a junior programmer wrote. They hinted there may be a problem and said it will be used heavily on large strings.

public string ReverseString(string sz)
{
    string result = string.Empty;
    for(int i = sz.Length-1; i>=0; i--)
    {
      result += sz[i]
    }
    return result;
}

I couldn’t spot it. I saw no problems whatsoever.
In hindsight I could have said the user should resize but it looks like C# doesn’t have a resize (i am a C++ guy).

I ended up writing things like use an iterator if its possible, [x] in containers could not be random access so it may be slow. and misc things. But I definitely said I never had to optimize C# code so my thinking may have not failed me on the interview.

I wanted to know, what is the problem with this code, do you guys see it?

-edit-

I changed this into a wiki because there can be several right answers.
Also i am so glad i explicitly said i never had to optimize a C# program and mentioned the misc other things. Oops. I always thought C# didnt have any performance problems with these type of things. oops.

  • 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-05-11T21:43:35+00:00Added an answer on May 11, 2026 at 9:43 pm

    A few comments on the answers given so far:

    • Every single one of them (so far!) will fail on surrogate pairs and combining characters. Oh the joys of Unicode. Reversing a string isn’t the same as reversing a sequence of chars.
    • I like Marc’s optimisation for null, empty, and single character inputs. In particular, not only does this get the right answer quickly, but it also handles null (which none of the other answers do)
    • I originally thought that ToCharArray followed by Array.Reverse would be the fastest, but it does create one “garbage” copy.
    • The StringBuilder solution creates a single string (not char array) and manipulates that until you call ToString. There’s no extra copying involved… but there’s a lot more work maintaining lengths etc.

    Which is the more efficient solution? Well, I’d have to benchmark it to have any idea at all – but even so that’s not going to tell the whole story. Are you using this in a situation with high memory pressure, where extra garbage is a real pain? How fast is your memory vs your CPU, etc?

    As ever, readability is usually king – and it doesn’t get much better than Marc’s answer on that front. In particular, there’s no room for an off-by-one error, whereas I’d have to actually put some thought into validating the other answers. I don’t like thinking. It hurts my brain, so I try not to do it very often. Using the built-in Array.Reverse sounds much better to me. (Okay, so it still fails on surrogates etc, but hey…)

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

Sidebar

Related Questions

I had telephone interview question yesterday. The interviewer asked me if I had faced
I asked a question like this in an interview for a entry level programmer:
I was asked this question in some interview. I was required to write code
Recently I had a question on the interview - I was asked to compare
Another recent C# interview question I had was if I knew what Boxing and
I had an interesting job interview experience a while back. The question started really
I just had a quick phone interview. The interviewer asked me a few questions
I was asked this question in an interview. Although the interview was for dot
I had a C++ interview recently where I was asked, how does the compiler
Recently in an interview I was asked to write a query where I had

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.