1. Problem understanding

We are given a string s and an integer pos. We need to find the character at the pos-th position (1-based indexing) in an infinitely generated string.

In every step i, the string is updated using this rule:

\[ S_{new}=S+\underbrace{\$\$\cdots\$}_{i\text{ times}}+reverse(S) \]

That means:

  1. Keep the current string.
  2. Append i dollar ($) characters.
  3. Append the reverse of the current string.

We must return the character at the given position without actually building the infinite string.

2. Example

Given:

Code
s = "12345", pos = 10

Initially:

12345

After Step 1:

12345$54321

PositionCharacter
11
22
33
44
55
6$
75
84
93
102
111

Output: 2

The answer is 2.

3. Why not build the string?

Suppose the initial string has length 5.

StepString length
Initial5
Step 111
Step 224
Step 351
Step 4105

The string grows very quickly. With pos up to \(10^9\), constructing and storing the string is inefficient.

Instead, we use mathematical length calculation and backward position mapping.

4. Core concept – Work backwards instead of forwards

At every expansion, the string has three parts:

Part 1: Previous string

Part 2: Added $ characters

Part 3: Reversed previous string

For example, if the previous string is "12345" and Step 1 adds one $:

Code
Previous string:  12345
Dollar section:       $
Reverse section:      54321

The new string is:

Code
12345$54321

We can locate the required position by checking which section it belongs to.

Three possible cases

Case 1: Position is in the previous string

If pos is within the previous string's length, we don't need to change the position. We simply move to the previous expansion level.

Case 2: Position is in the $ section

If the position falls between the previous string and its reversed copy, the answer is $.

Case 3: Position is in the reversed string

We convert the position into the corresponding position in the previous string using:

\[ \boxed{p_{\text{new}}=L-p+1} \]

Where:

  • \(L\) = current string length
  • \(p\) = current position
  • \(p_{\text{new}}\) = mapped position in the previous string

This allows us to move backwards without constructing or reversing strings.

5. C# code

C#
class Solution
{
    public char posInfinite(string s, int pos)
    {
        long length = s.Length;
        long p = pos;
        long step = 1;

        // Find the expansion level containing pos
        while (length < p)
        {
            length = 2 * length + step;
            step++;
        }

        // Trace the position backwards
        step--;

        while (step > 0)
        {
            long previousLength = (length - step) / 2;

            // Position is in the original string
            if (p <= previousLength)
            {
                length = previousLength;
                step--;
                continue;
            }

            // Position is in the '$' section
            if (p <= previousLength + step)
            {
                return '$';
            }

            // Position is in the reversed string
            p = length - p + 1;

            length = previousLength;
            step--;
        }

        // Position is in the initial string
        return s[(int)p - 1];
    }
}

6. Line-by-line code explanation

Step 1: Initialize variables

C#
long length = s.Length;
long p = pos;
long step = 1;
  • length stores the current string length.
  • p stores the target position.
  • step represents the number of $ characters added in the next expansion.

We use long for length and position calculations to avoid overflow during mathematical operations.

Step 2: Find the expansion level

C#
while (length < p)
{
    length = 2 * length + step;
    step++;
}

We don't generate the actual string. Instead, we calculate how long the string would become after each expansion.

The length formula is:

\[ L_{new}=2L_{old}+i \]

Where i is the current expansion step.

For s = "12345" and pos = 10:

ExpansionCalculationLength
Initial55
Step 12 × 5 + 111

Since length 11 is greater than or equal to position 10, we stop.

Step 3: Adjust the step counter

C#
step--;

During the previous loop, step was incremented after each expansion. This adjustment makes it represent the most recent expansion level we need to trace backwards.

Step 4: Trace backwards

C#
while (step > 0)
{
    long previousLength = (length - step) / 2;

We calculate the previous string's length using the reverse of the expansion formula:

\[ L_{old}=\frac{L_{new}-i}{2} \]

For Step 1:

\[ L_{old}=\frac{11-1}{2}=5 \]

So the previous string length is 5.

Step 5: Check the first part

C#
if (p <= previousLength)
{
    length = previousLength;
    step--;
    continue;
}

If the target position is inside the original portion of the expanded string, the position remains unchanged.

We simply move to the previous expansion level.

Step 6: Check the dollar section

C#
if (p <= previousLength + step)
{
    return '$';
}

The dollar section starts immediately after the previous string.

Its ending position is:

\[ L_{old}+i \]

If p falls in this section, the required character is $.

Step 7: Map the reversed section

C#
p = length - p + 1;

length = previousLength;
step--;

If the position falls in the reversed portion, we map it back into the previous string.

The formula:

\[ p_{new}=L-p+1 \]

After mapping, we reduce the current length and move to the previous step.

Step 8: Return the character

C#
return s[(int)p - 1];

Once we have traced the position back to the initial string, we return the corresponding character.

Since C# strings use zero-based indexing, we subtract 1 from the 1-based position.

7. Dry run – s = "12345", pos = 10

Initial expansion

Code
12345$54321

The length is 11 and the target position is 10.

SectionPosition range
Previous string1–5
$ section6
Reversed string7–11

Position 10 is in the reversed section.

We calculate:

\[ p=11-10+1=2 \]

Now the position is 2 in the original string "12345".

Code
12345
 ^
 Position 2

Result: 2

8. Dry run – s = "abc", pos = 7

Initial string:

Code
abc

After Step 1:

Code
abc$cba

Length = 7.

SectionPosition range
Previous string1–3
$ section4
Reversed string5–7

Position 7 is in the reversed section.

\[ p=7-7+1=1 \]

Now we trace back to position 1 of "abc".

Code
abc
^
Position 1

Result: a

9. Complexity analysis

ComplexityExplanation
Time: O(log(pos))The string length grows rapidly, and we only traverse the expansion levels.
Space: O(1)We use only a few variables without creating additional strings.

10. What to remember for interviews

  • Never construct a huge string if the problem only asks for one character.
  • Calculate the length of each expansion mathematically.
  • Identify whether the position belongs to the original string, the added characters, or the reversed section.
  • When the position is in the reversed section, map it back using length - pos + 1.
  • Work backwards until the position belongs to the initial string.

This is a mathematical position-mapping problem, not a string-construction problem.

You can use the same technique in recursive string problems where a string is expanded through mirroring, reversal or repeated concatenation.