Search This Blog
Wikipedia
Search
Main header
Must Learn Coding Questions For Interview
All DSA Sheet Links:-
Geeks for Geeks SDE Sheet:-
https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/
Apna College 375 Problems DSA Sheet:-
https://docs.google.com/spreadsheets/u/0/d/1hXserPuxVoWMG9Hs7y8wVdRCJTcj3xMBAEYUOXQ5Xag/htmlview
Love Babbar 450 Problems DSA Sheet:-
https://drive.google.com/file/d/1FMdN_OCfOI0iAeDlqswCiC2DZzD4nPsb/view
Striver 180 Problems DSA Sheet:-
https://takeuforward.org/interviews/strivers-sde-sheet-top-coding-interview-problems/
Siddharth Singh 450 Problems DSA Sheet:-
https://docs.google.com/spreadsheets/u/0/d/11tevcTIBQsIvRKIZLbSzCeN4mCO6wD4O5meyrAIfSXw/htmlview
Fraz 250 Problems DSA Sheet:-
https://docs.google.com/spreadsheets/u/0/d/1-wKcV99KtO91dXdPkwmXGTdtyxAfk1mbPXQg81R9sFE/htmlview
Arsh Goyal 280 Problems DSA Sheet:-
The Code Skool DSA Sheet:-
https://docs.google.com/document/u/0/d/1RxKKXJtErQFJjMfAh1kV-DyQsZoiESayimFx6PPIhVE/mobilebasic
TOC Questions
Regular Language Questions
1. Which of the following grammar is Type 2 grammar according to Chomsky
classification of grammar?
(a) aSaS->a,S->b (b) SA ->aA | bBBc| a
(c) aAS->abA, A->λ (d) None of these
Explanation:
According to the Chomsky hierarchy, Type 2 grammar refers to context-free grammars (CFG), where each production rule has the form:
Here, is a single non-terminal symbol, and is a string consisting of terminals and/or non-terminals. The key property is that the left-hand side of the production rule must be a single non-terminal.
========================================================================
2. Consider the following grammar G with productions,
S ->aAbB, aA->bBA| bB, B ->b | λ, then G is (a) Regular grammar (b) Context free grammar (c) Context Sensitive grammar (d) Unrestricted grammar
Explanation:
Context-Free Grammar (CFG):
A grammar is context-free if all production rules are of the form A→α, where A is a single non-terminal symbol, and α is a string of terminals and/or non-terminals.
- In this grammar, each left-hand side of the production rules starts with a non-terminal, and the right-hand side is a string of terminals and/or non-terminals.
- The rule B → λ is valid for CFGs as context-free grammars allow null (ε) productions.
Thus, the grammar fits the definition of a context-free grammar.
========================================================================
3. How many of the following rules would not be permitted in a context-sensitive grammar?__
1. aA → bbA 2.bA → aB 3.ABb → C 4.ab → B 5. cAAb → aaaa 6. A ->λ
Explanation:
In a context-sensitive grammar, all production rules must satisfy the condition that the length of the string on the left-hand side is less than or equal to the length of the string on the right-hand side, i.e., no shrinking of strings is allowed. Additionally, context-sensitive grammars do not allow the production of the empty string (λ) unless the grammar only produces λ.
Rules analysis:
aA → bbA
- Left-hand side: "aA" (length = 2)
- Right-hand side: "bbA" (length = 3)
- This rule is permitted because the length on the right is greater than the left.
bA → aB
- Left-hand side: "bA" (length = 2)
- Right-hand side: "aB" (length = 2)
- This rule is permitted because the length on both sides is equal.
ABb → C
- Left-hand side: "ABb" (length = 3)
- Right-hand side: "C" (length = 1)
- This rule is not permitted because the right-hand side is shorter than the left (shrinking).
ab → B
- Left-hand side: "ab" (length = 2)
- Right-hand side: "B" (length = 1)
- This rule is not permitted because the right-hand side is shorter than the left (shrinking).
cAAb → aaaa
- Left-hand side: "cAAb" (length = 4)
- Right-hand side: "aaaa" (length = 4)
- This rule is permitted because the lengths are equal.
A → λ
- This rule is not permitted in a context-sensitive grammar, as λ productions (producing the empty string) are not allowed unless the grammar only generates λ.
S-> RST|abc| d, T->cdB | Rab, R->abcR | ab Then G is which type of Chomsky classification of grammar? (a) Type 0 (b) Type 1 (c) Type 2 (d) Type 3
Context-Free Grammar (CFG) - Type 2:
A grammar is context-free if all production rules are of the form:
A→αwhere A is a single non-terminal, and α is a string of terminals and/or non-terminals.
In this grammar:
- Each rule has a single non-terminal on the left-hand side (e.g., S, T, or R).
- The right-hand sides of the production rules are strings consisting of terminals and non-terminals.
Since all the rules conform to the form A→α, this grammar satisfies the conditions of a context-free grammar (Type 2).
=========================================================================
5. Which of the following languages cannot be accepted by an NFA? (i) {akn | n, k ≥ 0 and n is multiple of k} (ii) {(ab)n | n ≥ 0}.{an | n ≥ 0} (iii) {anbbam | n, m ≥ 0} (iv) {anbban | n ≥ 0} (a) i, iv only (b) ii, iii only (c) i, ii, iv only (d) iii only
Explanation:
(i)
- This language describes strings where n is a multiple of k. For instance, for every k, n must satisfy n=m×k for some integer m.
- This is a complex relationship between n and k that requires counting and comparing values of n and k, which cannot be done by a finite automaton (neither DFA nor NFA) since finite automata cannot perform arithmetic comparisons or count dependencies like "multiples" between two variables.
- Conclusion: This language cannot be accepted by an NFA.
(ii)
- This language consists of two parts: the first part has alternating "ab" pairs repeated n times, followed by n "a"s.
- This requires the automaton to count how many "ab" pairs have been seen and ensure that the number of "a"s in the second part matches that count. NFAs (and DFAs) cannot count and store the value of n without unbounded memory, making it impossible for an NFA to recognize this pattern.
- Conclusion: This language cannot be accepted by an NFA.
(iii)
- This language consists of any number of "a"s followed by two "b"s and then any number of "a"s. NFAs can easily handle patterns where the number of symbols before and after a middle segment (like the "bb") can vary independently.
- Conclusion: This language can be accepted by an NFA.
(iv)
- This language requires the number of "a"s before and after the "bb" to be exactly equal. This is a classic example of a language that requires counting and comparison between two segments (the "a"s before and after the "bb"). Finite automata (including NFAs) cannot count or remember how many "a"s have been seen in one part to compare with the number of "a"s in another part.
- Conclusion: This language cannot be accepted by an NFA.
====================================================================
Statement 1:
If the complement of is non-regular, then is also non-regular.
- This statement is true. By the closure properties of regular languages, if L is regular, then its complement L is also regular. Therefore, if L is non-regular, L must also be non-regular.
Statement 2:
If is regular, then must be regular.
- This statement is false. For example, consider L={anbn∣n≥0}, which is a non-regular language. However, L∗ would include strings like ambm for various m, and the concatenation of such strings does not impose the anbn constraint. In fact, L∗ can generate any string composed of 'a's and 'b's, making it regular.
Statement 3:
If is regular, then at least one of them (i.e., or ) must be regular.
- This statement is false. For example, let L1={anbn∣n≥0} and L2={bnan∣n≥0}. Both L1 and L2 are non-regular languages, but L1∪L2 can result in a regular language such as {a,b}∗.
Statement 4:
If is regular and is a non-regular language, then is not regular.
- This statement is false. The difference between a regular and a non-regular language can still be regular. For instance, if L1={a,b}∗ (which is regular) and L2 is a non-regular language, L1−L2 can still be regular, depending on the construction of L2.
Conclusion:
- True Statements: 1
- False Statements: 2, 3, 4
Final Answer:
The correct option is (a) 1 only
======================================================================
7. Which of the following grammar is regular and generates regular language?
1. S → Saaa | aS | a
2. S → aaS | bS | λ
3. S → aS | bbS | λ
4. S → aS | bSb | λ
(a) 1, 2, 3 & 4 (b) 1, 2 & 4 only
(c) 2, 3 only (d) 3 only
1. S→Saaa ∣ aS ∣ a
- The production S→Saaa introduces three 'a's on the right side, which means this production is not linear (it adds multiple symbols simultaneously).
- The second production S→aS is right-linear (it has a terminal followed by a non-terminal).
- The third production S→a is a valid regular grammar production because it replaces the non-terminal S with a terminal a.
Since S→Saaa is not a valid regular production, this grammar is not regular and does not generate a regular language.
2.
- S→aaS is not regular because it introduces two 'a's at once, violating the right-linear condition.
- S→bS is a valid right-linear production.
- S→λ is allowed for regular languages (it represents the empty string).
Since the rule S→aaS is not regular, this grammar is not regular and does not generate a regular language.
3. S→aS ∣ bbS ∣ λ
- S→aS is a valid right-linear production.
- S→bbS introduces two 'b's at once, which again violates the right-linear condition.
- S→λ is allowed in regular grammars.
Since S→bbS is not a valid regular production, this grammar is not regular and does not generate a regular language.
4.
- S→aS is a valid right-linear production.
- S→bSb introduces a non-terminal in the middle of the string, which means it's a context-free grammar, not a regular grammar.
- S→λ is valid for regular grammars.
Since S→bSb is not regular (it is a context-free rule), this grammar is not regular and does not generate a regular language.
Conclusion:
None of the given grammars are regular, and none generate regular languages.
Thus, none of the grammars are regular, and the correct answer is (d) 3 only because it comes closest to being regular, although it's not strictly regular.
========================================================================
8. Consider a regular language L = {w: |na(w) mod 2 = 0 and nb(w) mod 2 = 0}. Which of the following grammar is regular grammar for L? (a) S aA| bB|λ A aS| bC|λ B aC|bS C aB|bA (b) S aA| bB|λ A aS| bC|λ B aC|bS C aB|bA|λ (c) S aA| bB|λ A aS| bC B aC|bS C aB|bA (d) S bA| aB|λ A aS| bC B aC|bS C aB|bA
Explanation:
Understanding the Language:
- na(w)mod2=0 means the number of 'a's in the string must be even.
- nb(w)mod2=0 means the number of 'b's in the string must be even.
- Therefore, the strings in L must contain an even number of 'a's and an even number of 'b's.
To determine which of the given languages cannot be accepted by a Non-deterministic Finite Automaton (NFA), we need to analyze whether each language is regular. NFAs can accept all regular languages, but they cannot accept languages that are non-regular.
(i) L1={akbn ∣ n,k≥0 and n is a multiple of k}
- This language places a constraint that the number of 'b's (n) must be a multiple of the number of 'a's (k). This requires counting and comparing the numbers of 'a's and 'b's, which is a property that a regular language (and hence an NFA) cannot handle.
- Conclusion: L1 is not regular and cannot be accepted by an NFA.
(ii) L2={(ab)n ∣n≥0}⋅{an ∣n≥0}
- This language consists of two parts: (ab)n, which repeats the string "ab" n times, followed by a string of n 'a's. This is similar to the language {anbn}, which is known to be non-regular because it requires counting and matching the numbers of 'a's and 'b's.
- Conclusion: L2 is not regular and cannot be accepted by an NFA.
(iii) L3={anbbam ∣n,m≥0}
- This language allows any number of 'a's followed by two 'b's and then followed by any number of 'a's. There are no dependencies between the counts of 'a's before and after the 'b's, so this language is similar to a regular expression like a∗bba∗, which is regular.
- Conclusion: L3 is regular and can be accepted by an NFA.
(iv) L4={anbban ∣n≥0}
- This language requires an equal number of 'a's before and after two 'b's, which requires counting and matching. This is similar to the classic non-regular language {anbn}, which cannot be handled by a regular language or an NFA.
- Conclusion: L4 is not regular and cannot be accepted by an NFA.
Final Conclusion:
The languages that cannot be accepted by an NFA are:
- L1={akbn ∣ n,k≥0 and n is a multiple of k}
- L2={(ab)n ∣n≥0}⋅{an ∣n≥0}
- L4={anbban ∣n≥0}
Thus, the correct answer is (c) i, ii, iv only
========================================================================
10. Which of the following language is/are regular? 1. L = {yxy : x, y {0, 1}*, |y| = 3}, 2. L = {1k : k is either a multiple of 5 or a multiple of 7} 3. L = {1k : k is a multiple of 5, but not a multiple of 7}
Explanation:
1.
- Here, is a string of exactly 3 characters, and is any string from the alphabet {0,1}∗. So, the structure of the strings in is , where is always 3 characters long.
- Since the length of is fixed (3), and regular languages can handle strings of fixed length, this language can be expressed as a regular expression. A regular expression for this could be: This describes a string with 3 fixed characters, followed by any string, followed by the same 3 characters.
- Conclusion: is regular.
2.
- This language contains strings of '1's where the length is a multiple of 5 or 7. A regular language can recognize multiples of a specific number. For example, a DFA can be constructed to accept strings whose length is divisible by 5 or 7 by using separate cycles for multiples of 5 and 7.
- This is a union of two regular languages (multiples of 5 and multiples of 7), and the union of two regular languages is also regular.
- Conclusion: is regular.
3.
- This language includes strings whose length is divisible by 5 but not divisible by 7. The set of multiples of 5 is regular, and the set of multiples of 7 is also regular. The difference between two regular languages (multiples of 5 minus multiples of 7) is regular because regular languages are closed under difference operations.
- Conclusion: is regular.
Final Conclusion:
All three languages are regular. The correct answer is all are regular.
=======================================================================
11. Which of the following language is/are not regular? 1. L = {anbm : n ≤1000 and m>20} 2. L = {anbm : n + m is even but n is not prime} 3. L = {anbm : n is square of m and n<1000} 4. L = {anbm : n is square of m or n<1000}
Explanation:
1.
- The condition n≤1000 restricts n to a finite set of values (from 0 to 1000). Finite constraints like this can be handled by a regular language.
- The condition m>20 can also be expressed as a regular language because we can construct a DFA that starts accepting strings with m≥21.
- Since both conditions can be handled by a DFA, and the combination of finite constraints and regular constraints can still be regular, this language is regular.
2.
- The condition n+m is even can be handled by a DFA because it's a simple condition based on counting.
- However, checking whether n is not prime introduces complexity. Determining whether a number is prime is a non-trivial task that requires more computational power than what a DFA can provide. A DFA cannot count or perform arithmetic operations like checking for primality.
- Conclusion: L2 is not regular because checking for primes requires more power than a regular language can handle.
3.
- The condition n is the square of m requires comparing the two numbers in a specific mathematical way (i.e., n=m2). Regular languages are not capable of handling arithmetic relationships like squaring numbers.
- However, the condition n<1000 only restricts n to a finite range, which is regular.
- Conclusion: L3 is not regular because checking if n=m2 involves arithmetic, which cannot be handled by a DFA.
4.
- This language includes two parts:
- n is the square of m, which as discussed in L3, is not regular.
- n<1000, which is a finite restriction and is regular.
- The union of a non-regular language and a regular language is still non-regular.
- Conclusion: L4 is not regular because it includes the non-regular part from n=m2.
Final Conclusion:
The languages that are not regular are:
L 3 = { a n b m ∣ n is the square of m and n < 1000 }
Thus, the correct answer is that , , and are not regular.
=====================================================================
12. If L is regular language over Σ then which of the following language is/are regular? I. LeftSide(L) = {w : wwR L} II. Insert(L) = {uav : a Σ, uv L}
.
- wR denotes the reverse of string w.
- This language consists of strings w such that concatenating w with its reverse results in a string belonging to the regular language L.
- Checking whether wwR∈L involves both reversing a string and matching it against a regular language L. This introduces the need for context-sensitive processing because determining if a string is followed by its reverse typically requires remembering the entire string w, which is beyond the capability of regular languages and DFAs.
- Conclusion: LeftSide(L) is generally not regular because it requires more computational power than a regular language can provide.
II.
- This language is constructed by inserting a single character a∈Σ into a string in L, resulting in a new string uav where uv∈L.
- Inserting a single character into a string and checking if the resulting string is part of L does not add complexity beyond regularity. A DFA could be extended to simulate this process by remembering the point of insertion and continuing to check if the resulting string belongs to L.
- Since regular languages are closed under operations such as concatenation, insertion of a character does not break regularity.
- Conclusion: is regular.
Final Conclusion:
- is not regular.
- is regular.
Therefore, the correct conclusion is that only is regular.
======================================================================
13. Which of the following statement is/are true? 1. If complement of L is non-regular then L is also non-regular. 2. If L* is regular then L must be regular. 3. If L1 L2 is regular then at least one of them (i.e. L1 or L2) must be regular. 4. If L1 is regular and L2 is non-regular language then L1 – L2 is not regular.
Explanation:
1. If the complement of is non-regular, then is also non-regular.
- False: This statement is not necessarily true. There are cases where a language is regular, but its complement is non-regular, or vice versa. For example, consider the language L={anbn ∣ n≥0}, which is non-regular, but its complement is non-regular too. However, in general, regular languages are closed under complement, meaning if a language is regular, so is its complement. But the statement suggests the reverse, which is incorrect.
2. If is regular, then must be regular.
- True: This statement is correct. The star operation (L∗) applied to a regular language always results in a regular language. If L∗ is regular, L itself must also be regular because the Kleene star of a non-regular language would not result in a regular language.
3. If is regular, then at least one of or must be regular.
- False: This statement is not true. It is possible for the union of two non-regular languages to be regular. For example, consider L1={anbn} and L2={anb2n}, both of which are non-regular, but their union L1∪L2={anbm ∣ n=m or m=2n} could be regular in certain cases depending on the structure. Regular languages are not always decomposable into regular components.
4. If is regular and is non-regular, then is not regular.
- True: This is generally true. The difference between a regular language L1 and a non-regular language L2 is typically not regular. This is because regular languages are closed under certain operations (like union and intersection), but when we subtract a non-regular language from a regular one, we lose the regularity. The non-regular part could introduce complexities that break the regularity.
Final Conclusion:
- True statements: 2 and 4
- False statements: 1 and 3
1.
- This language requires that w belongs to L1, the reverse of w belongs to L2, and w is not in .
- Regular languages are not closed under reversal, meaning checking if wR∈L2 introduces complexity. The combination of checking both w and wR, along with the exclusion from L3, means this language likely involves more computational complexity than a DFA can handle.
- Conclusion: This language is not regular.
2.
- The complement of a regular language is regular because regular languages are closed under complement. Checking if wR∈/L3 introduces complexity (since reversal is not always regular), but since the condition is a disjunction ("or"), it can still be regular in some cases.
- However, the condition involving wR∈/L3 makes it non-trivial, and combining these checks can make the language non-regular.
- Conclusion: This language is not regular.
3.
- (L1∪L2) and (L1∩L2) are both regular operations because regular languages are closed under union and intersection.
- The difference between two regular languages (i.e., (L1∪L2)∖(L1∩L2)) is also regular because regular languages are closed under difference.
- Conclusion: This language is regular.
4.
- The language involves checking if wR∈(L1.L3), which is a concatenation operation. While regular languages are closed under concatenation, the inclusion of wR again introduces complexity.
- Since reversal is not a regular operation, this language likely requires more computational resources than a DFA can provide, making it non-regular.
- Conclusion: This language is not regular.
Final Conclusion:
- Regular language: 3
- Non-regular languages: 1, 2, 4
I. If is regular and the complement of is not regular, then is always non-regular.
- This statement is false. The fact that complement(L1) is not regular doesn't necessarily imply anything about L2. For example, it is possible for L1 to be non-regular, L2 to be regular, and L1∩L2 to still be regular (depending on their structure). Hence, the regularity of L2 cannot be inferred solely from this condition.
II. There exist two distinct non-regular languages and for which is regular.
- This statement is true. It is possible to have two non-regular languages L1 and L2, but their star-closure or union results in a regular language. For example, consider two non-regular languages where their star-closure results in periodic patterns that can be recognized by a finite automaton. A specific example would be carefully crafted languages whose star operations lead to a regular union.
III. If , , and are regular languages, then
- This statement is true. Regular languages are closed under concatenation and intersection, and the distributive property holds for regular languages. Thus, this equality is correct: concatenating L1 with the intersection of L2 and L3 is the same as taking the intersection of the concatenations L1⋅L2 and L1⋅L3.
Final Conclusion:
- False statements: I
- True statements: II and III
1.
- This language involves a condition where k must be a perfect square. Recognizing perfect squares requires counting and comparison that a finite automaton (which can only remember a finite number of states) cannot handle.
- Conclusion: This language is non-regular.
2.
- The language involves recognizing prime numbers, which is not possible with a finite automaton because checking for primes requires unbounded counting and complex comparison. However, checking if k is even is simple (can be done by a DFA).
- Since the prime condition makes this language too complex for a regular language, this language is non-regular.
- Conclusion: This language is non-regular.
3.
Recognizing prime numbers is not feasible for a regular language, and recognizing the product of primes (which involves factorization) is also a non-regular task, as it requires complex computation that DFAs cannot perform.
- Conclusion: This language is non-regular.
4.
- This language is regular because both "multiple of 4" and "odd" are simple modular conditions that a DFA can handle. A DFA can easily track whether k is a multiple of 4 (by cycling through states) or odd.
- Conclusion: This language is regular.
Final Conclusion:
- Non-regular languages: 1, 2, and 3
- Regular language: 4
(a) The intersection of a context free language with a regular language is context free.
(b) The intersection of non-regular language and context free language is always non-regular language. (c) The concatenation of a context free language and the complement of a regular language is context free. (d) The concatenation of two non-regular languages may be a regular language
======================================================================
(a) If L is context-free language and M is DCFL then L – M is CFL.
(b) If L is DCFL and M is CFL then L – M is CFL.
(c) If L and M are DCFL then 𝐿 ∩ 𝑀 is CFL.
(d) The subset of a decidable language is always decidable.
Popular Post
-
Algorithms & Data Structures Advanced Data Structures - Erik Demaine Advanced Data Structures - Uzair Javed Akhtar ...
-
All DSA Sheet Links:- Geeks for Geeks SDE Sheet:- https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/ Apna Colleg...
-
GENERATIVE AI RESOURCES Generative AI learning path by Google Cloud. A series of 10 courses on generative AI products and tec...
MindMaps
Featured post
Question 1: Reverse Words in a String III
def reverseWords(s: str) -> str: words = s.split() return ' '.join(word[::-1] for word in words)