GATE / CS ARCHIVE

SIMILAR QUESTIONS / THEORY OF COMPUTATION

Test nesting of four-block count equalities

4 questions solved the same way, newest first. Back to Theory of Computation

How these are solved

A single stack handles nested or independent adjacent matches, not crossed independent equalities. Linear additive variants require a separate push/pop invariant; compare constraints, not variable spelling.

2 marks · MSQ
Report issue

Context-free constraints on four blocks

Context-Free Grammars & Languages

Question 48, GATE 2026: Consider strings a^i b^j c^k d^ℓ with i,j,k,ℓ≥0. Which restrictions yield context-free languages? A i+k=j+ℓ; B i=k and j=ℓ; C i=ℓ and j=k; D i+j=k+ℓ.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A, C, D

Givens and goal

Consider strings ai bj ck d^ℓ with i,j,k,ℓ≥0. Which restrictions yield context-free languages? A i+k=j+ℓ; B i=k and j=ℓ; C i=ℓ and j=k; D i+j=k+ℓ.

Approach

Give stack constructions for the positive choices and a pumping-lemma contradiction for the crossing equalities.

Step-by-step solution

1. For A, scan the string while tracking block order a*b*c*d* in finite control. Maintain the signed difference (#a+#c)−(#b+#d). Store its sign in the finite state and its absolute value as repeated stack symbols. On a or c, add 1; on b or d, subtract 1, popping if this cancels the current sign and pushing otherwise. Accept exactly when the final difference is zero. This is a pushdown automaton, so A is context-free.
2. For D, check block order, push one marker for every a or b, and pop one for every c or d. Reject an attempt to pop an empty counter and accept exactly with zero balance at the end. This enforces i+j=k+ℓ.
3. For C the equalities are nested. The grammar S→aSd|T, T→bTc|ε generates precisely ai bj cj di: outer recursion matches a with d, inner recursion matches b with c. Hence C is context-free.
4. For B suppose a context-free pumping length p exists, and take w=ap bp cp dp. Every decomposition w=uvxyz with |vxy|≤p and |vy|>0 has its pumping window in one block or at most two adjacent blocks. It cannot reach both members of either matching pair a/c or b/d.
5. Pump v and y away, using exponent 0. At least one block count decreases, and its matching nonadjacent block is untouched. The equality i=k or j=ℓ therefore fails. This contradicts the pumping lemma for every allowed decomposition, so B is not context-free.
6. The correct selections are A,C,D. Two independent crossing counters cannot in general be handled by the single last-in-first-out store used for nested matching.

Original paper, page 34 · Answer key

2 marks · MCQ
Report issue

Which counting languages are context-free

Context-Free Grammars & Languages

Question 35, GATE 2018: Over words a^m b^n c^p d^q, with all exponents nonnegative, define I m+p=n+q; II m=n and p=q; III m=n=p and p≠q; IV mn=p+q. Which are context-free? A I,IV; B I,II; C II,III; D II,IV.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Method

One stack can track a single signed difference, so the crossed-looking equality in I is still manageable; a finite-state component checks block order. II has grammar S→AB, A→aAb|ε, B→cBd|ε. For III, restricting to positive a,b,c blocks and no d leaves the standard non-CFL equal-three-count language. For IV, the multiplication constraint cannot be reduced to one additive counter. The pumping argument below makes this precise, including decompositions that cross a block boundary rather than assuming where the pumped substrings lie.

Solution

I is context-free: a PDA tracks the signed running difference(#a+#c)−(#b+#d) with one stack symbol for positive excess and another for negative excess, canceling opposite contributions; finite control enforces a*b*c*d* order. II is the concatenation{an bn}{cp dp}, a CFL. For III, intersect with a+b+c+ (no d); the result is{an bn cn: n≥1}, not context-free. For IV, suppose pumping length p and take ap bp cp². Pumping up either disrupts block order or adds α a symbols, β b symbols and γ c symbols, with α+β+γ≤p and at least one added symbol. If α+β=0, the product stays p² but γ>0. Otherwise the required increase in c is p(α+β)+αβ≥p, while actual γ≤p−α−β<p. In every case the equality mn=p+q fails (here the fourth-block length remains 0). Thus only I and II are CFL, B.

Original paper, page 14 · Answer key

2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 44, GATE 2015: Which languages are context-free? L1={a^m b^n a^n b^m | m,n≥1}; L2={a^m b^n a^m b^n | m,n≥1}; L3={a^m b^n | m=2n+1}. Exponents denote nonnegative integer counts unless constrained otherwise. Options: (A) L1 and L2 only; (B) L1 and L3 only; (C) L2 and L3 only; (D) L3 only.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. L1=am bn an bm has nested dependencies: S→aSb|aTb and T→bTa|ba generates m, n≥1, so it is CFL.

2. L3 is generated by U→aaUb|a for n≥0. L2=am bn am bn is not CFL: for pumping length p, choose ap bp ap bp. The pumping window of length≤p touches at most two adjacent blocks, so pumping changes at least one block but cannot change its corresponding nonadjacent equal block; if a pumped piece crosses a boundary, pumping can also destroy the four-block form. Thus pumping fails. L1, L3 only, B.

Original paper, page 68 · Answer key

1 mark · MCQ
Report issue

Context-free block-count languages

Context-Free Grammars & Languages

Question 19, GATE 2006: For n,m≥0 define L₁={0^(n+m)1^n0^m}, L₂={0^(n+m)1^(n+m)0^m}, and L₃={0^(n+m)1^(n+m)0^(n+m)}. Which languages are not context-free?
Options:
(A) L₁ only
(B) L₃ only
(C) L₁ and L₂
(D) L₂ and L₃
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. L1={0n+m1n0m} is context-free: S→0S0|T and T→0T1|ε generates it. Write L2={0k1k0m:0≤m≤k}.

2. It is not context-free: apply the CFL pumping lemma to 0p1p0p. A pump confined to one block breaks either equality of the first two block lengths or the last≤first inequality (choose pumping direction accordingly).

3. A pump crossing one boundary cannot touch all three blocks; preserving the first two lengths while pumping down makes the third too long, while other changes break equality or block order. L3 is{0k1k0k:k≥0}, also non-context-free by the usual three-block pumping argument.

4. Hence L2 and L3, D.

5. Teaching check: For L₂ the two equal leading block lengths and the upper bound on the final block are linked. A pumping window of length p cannot span all three p-length blocks in 0p1p0p. Even if it changes the first two blocks equally, pumping down leaves the untouched third block too long.

Original paper, page 3