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:
- Keep the current string.
- Append
idollar ($) characters. - 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:
s = "12345", pos = 10Initially:
12345
After Step 1:
12345$54321
| Position | Character |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | $ |
| 7 | 5 |
| 8 | 4 |
| 9 | 3 |
| 10 | 2 |
| 11 | 1 |
Output: 2
The answer is 2.
3. Why not build the string?
Suppose the initial string has length 5.
| Step | String length |
|---|---|
| Initial | 5 |
| Step 1 | 11 |
| Step 2 | 24 |
| Step 3 | 51 |
| Step 4 | 105 |
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 $:
Previous string: 12345
Dollar section: $
Reverse section: 54321The new string is:
12345$54321We 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
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
long length = s.Length;
long p = pos;
long step = 1;lengthstores the current string length.pstores the target position.steprepresents 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
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:
| Expansion | Calculation | Length |
|---|---|---|
| Initial | 5 | 5 |
| Step 1 | 2 × 5 + 1 | 11 |
Since length 11 is greater than or equal to position 10, we stop.
Step 3: Adjust the step counter
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
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
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
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
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
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
12345$54321The length is 11 and the target position is 10.
| Section | Position range |
|---|---|
| Previous string | 1–5 |
$ section | 6 |
| Reversed string | 7–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".
12345
^
Position 2Result: 2
8. Dry run – s = "abc", pos = 7
Initial string:
abcAfter Step 1:
abc$cbaLength = 7.
| Section | Position range |
|---|---|
| Previous string | 1–3 |
$ section | 4 |
| Reversed string | 5–7 |
Position 7 is in the reversed section.
\[ p=7-7+1=1 \]
Now we trace back to position 1 of "abc".
abc
^
Position 1Result: a
9. Complexity analysis
| Complexity | Explanation |
|---|---|
| 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.


Join the conversation! Your thoughts help the community grow.