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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:07:18+00:00 2026-06-01T02:07:18+00:00

Hi i have a function that takes an integer number n and returns a

  • 0

Hi i have a function that takes an integer number n and returns a string that contains the numbers from 1 to n separated by ‘,’. Now this number integer n could be any number as large as 1 billion. What could be the best solution for this. And how do i manage the memory related issues like if the RAM is just 2 Gb so what would happen if i return this big string in C#. The function signature is:

string convtostr (int n)
{}

so the input for e.g. could be n = 5, then output would be a string like this
“1,2,3,4,5”

  • 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-01T02:07:20+00:00Added an answer on June 1, 2026 at 2:07 am

    If your method footprint must exactly match that which you show, then it cannot be done. Because you cannot inherit String and make a new better type that will work for >2GB

    If your assumption about the maximum input value of n is incorrect then you can provide a working solution to match the footprint. Such as if the max value of n was 10000 or something very small.

    However I think the IEnumerable<char> approach as suggested by @MikeSamuel is the only answer that would present a working solution that returns close to the stated requirements.

    As you have tagged this c# 4.0 Most likely they were looking for a solution that used yield syntax such as:

    static IEnumerable convtostr(int n)
    {
        for (int i = 1; i < n; i++)
            yield return string.Format("{0}, ", i.ToString());
        yield return n.ToString();
    }
    

    you can then explain that you use it like the following

    int input = 1000000000; // 1 billion.
    
    foreach (var value in convtostr(input))
    {
        Console.Write(value );
    }
    

    And that is likely what they were wanting you to answer with. As this will work with very little active RAM by not actually storing the whole final string in memory.

    Edit: another answer similar to that suggested by @suddnely_me is to pass in an action for each number instead, such as:

    static void convtostr(int n, Action<int> action)
    {
        for (int i = 1; i <= n; i++)
            action(i);
    }
    

    This is then called using:

    int input = 1000000000; // 1 billion.
    convtostr(input, n =>
        {
            if (n < input) { Console.Write("{0}, ", n); }
            else { Console.Write(n); }
        });
    

    Which will actually perform almost the same as using the yield syntax.

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

Sidebar

Related Questions

I have to create a function bitParity(int x) that takes an integer and returns
I have a function that takes a string and returns a string. In it,
I have a calc function in java script that takes three integer parameters, following
I have a function that takes a string-like argument. I want to decide if
I have a function that takes variadic arguments, that I obtain from func_get_args() .
I have Clojure function that takes a sequence of numbers chops it into the
I have a function that takes in a number of parameters.. it then does
I have a function similar to the following that takes an integer as a
I have this Array i wrote a function MostFreq that takes an array of
I have a function that takes, amongst others, a parameter declared as int privateCount

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.