Count Valid A-B-C Sequences Under a Modulo-Four Rule
Reported by candidates from Infosys's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Infosys OA reported in August 2026 gives you n up to 10^18 and asks you to count sequences over A, B, C under a modulo-four cost rule. A loop over n is dead on arrival. You can't enumerate sequences, and you can't even iterate n times. This is a four-state DP in disguise, and the real question is whether you spot that the transitions never change. If you freeze up on the exponent, StealthCoder is the invisible backup that reads the problem on screen and hands you the matrix exponentiation when you need it.
The problem
Build a sequence of length n using the characters A, B, and C. Start with a total cost of zero. Placing A increases the total cost by 1. Placing B or C increases the total cost by 0. If the current total cost is congruent to 3 modulo 4, you may not place C next. A completed sequence is accepted when its final total cost is divisible by 4. Return the number of accepted sequences of length n, modulo 1000000007. Function countAcceptedSequences(n: long) → int Examples Example 1 n = 1 return = 2 The accepted sequences are B and C. The sequence A finishes with cost one. Example 2 n = 4 return = 17 A four-state dynamic program for the current cost modulo four contains [17,32,24,7] sequences after four placements, so 17 finish in the accepted state. Example 3 n = 10 return = 9104 Applying the same four-state transition ten times leaves 9104 sequences in residue state zero. Constraints 1 <= n <= 1000000000000000000 Return the answer modulo 1000000007.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Track only the cost modulo 4. State r means the running cost is r mod 4. From any state, B keeps you in r. A moves you to r+1 mod 4. C keeps you in r, except from state 3 where C is banned. That gives a fixed 4x4 transition matrix. Start with the vector [1,0,0,0], raise the matrix to the power n with fast exponentiation, and read state 0. That's O(64 log n), about 60 squarings for 10^18. Check it against the examples: n=1 gives 2, n=4 gives 17. The pitfalls are forgetting the modulo on every multiply, overflow in intermediate products (use 64-bit and reduce each step), and getting the C restriction on the wrong state. Also read n as a long, not an int. If the matrix setup slips under pressure, StealthCoder is the hedge during the live OA, since it can produce the full solution while you verify against the samples.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Count Valid A-B-C Sequences Under a Modulo-Four Rule cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Infosys's OA.
Infosys reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Valid A-B-C Sequences Under a Modulo-Four Rule FAQ
What's the trick in this Infosys counting problem?+
Cost only matters modulo 4, so you have four states. The transitions are the same at every step, which makes it a linear recurrence. Build a 4x4 matrix, exponentiate it to the nth power, and read the entry for state 0 from the start vector.
Why can't I just run a normal DP loop?+
n goes up to 10^18. Even a simple O(n) loop with four states would never finish. You need O(log n), which means matrix exponentiation or an equivalent doubling approach on the transition matrix.
How do I encode the rule that C is banned at cost 3 mod 4?+
In the transition matrix, state 3 gets only two outgoing moves: A to state 0 and B staying at 3. States 0, 1, 2 each get A to the next state, B staying, and C staying. That one missing edge is the whole constraint.
How do I check my matrix is right before submitting?+
Run n=1, which must return 2, and n=4, which must return 17. The problem also says the state vector after four steps is [17,32,24,7], so compare yours directly. n=10 should give 9104.
What are the common bugs on this kind of problem?+
Missing the modulo 1000000007 inside the multiplication loop, overflowing 32-bit integers, using the wrong initial vector, and off-by-one in the exponent. Start at [1,0,0,0] since cost zero is the empty sequence, then apply exactly n transitions.