Automata Questions
1. Given an n-by-n matrix of 0’s and 1’s where all 1’s in each row come before all 0’s, find the most
efficient way to return the row with the maximum number of 0’s.
2. Print a given matrix in spiral form
Given a 2D array, print it in spiral form.
3. A Pythagorean triplet is a set of three integers a, b and c such that a2 + b2 = c2. Given a limit, generate all
Pythagorean Triples with values smaller than given limit.
4. Find the distinct elements in a given array. (Assume size of an array n<=20)
5. Program to sort array in ascending & descending order.
Input:
5
86927
Output:
26789
98762
6. Print the following pattern pattern
Input:
3 4
Output:
3
44
555
6666
555
44
3
Input :
4 4
Output:
4
55
666
7777
666
55
4
7. Print the below pattern
Input:
4
Output:
1*2*3*4*17*18*19*20
– -5*6*7*14*15*16
– – – -8*9*12*13
– – – – – -10*11
8. Paranthesis checker: Check whether the given expression is valid or not(only parenthesis symbol).
Test Case: 1
Input: “(( ))”
Output: Valid
Test Case: 2
Input: “()(“
Output: Invalid
9. Print the following Pattern and get the output to match test cases?
To print the pattern like
for n=3
the program should print
1112
3222
3334
10. To print the trapezium pattern?
Please also post your code in the comments in different languages or same languages with short or
better time complexity code.
If N = 4
1*2*3*4*17*18*19*20
5*6*7*14*15*16
8*9*12*13
10*11
If n = 5
1*2*3*4*5*26*27*28*29*30
6*7*8*9*22*23*24*25
10*11*12*19*20*21
13*14*17*18
15*16
If N = 2
1*2*5*6
3*4
11. Programming Pattern to Print 2*N Number of rows for input Pattern?
3
44
555
6666
555
44
3
12. Program to print the given 2D Array or Matrix in spiral form
Input:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
13. A n-by-n matrix of 0’s and 1’s is given, where all 1’s in each row come before all 0’s, find the most
efficient way to return the row with the maximum number of 0’s.
14. A Pythagorean triplet is a set of three integers a, b and c such that a2 + b2 = c2. Given a limit, generate all
Pythagorean Triples with values smaller than given limit. Limit = 20.
15. Write a program to find the sum of contiguous sub array within a one-dimensional array of numbers
which has the largest sum.
16. Write a program to find the GCD of two Integers.
17. Write a program to remove all vowels from a string.
18. Write a program to return a sorted array from two unsorted array.
19. Write a program to find out sum of digits of given number.
This is a tricky question to solve. The algorithm to solve the question is given below:
1) Take the integer as input.
2) Divide the input integer by 10, obtain its remainder and quotient.
3) Increment the new variable with the remainder obtained in step 2.
4) Repeat the step 2 & 3 with the quotient obtained until the quotient becomes zero.
5) Print the output and exit.
20.
Consider the DFA given.
Which of the following are FALSE?
1. Complement of L(A) is context-free.
2. L(A) = L((11*0+0)(0 + 1)*0*1*)
3. For the language accepted by A, A is the minimal DFA.
4. A accepts all strings over {0, 1} of length at least 2.
21. Given the language L = {ab, aa, baa}, which of the following strings are in L*?
1) abaabaaabaa
2) aaaabaaaa
3) baaaaabaaaab
4) baaaaabaa
22. Consider the set of strings on {0,1} in which, every substring of 3 symbols has at most two zeros. For
example, 001110 and 011001 are in the language, but 100010 is not. All strings of length less than 3 are
also in the language. A partially completed DFA that accepts this language is shown below.
The missing arcs in the DFA are
23. Definition of a language L with alphabet {a} is given as following. L={ | k>0, and n is a
positive integer constant}
What is the minimum number of states needed in DFA to recognize L?
24. A deterministic finite automation (DFA)D with alphabet {a,b} is given below
Which of the following finite state machines is a valid minimal DFA which accepts the same language as
D?
25. Let w be any string of length n is {0,1}*. Let L be the set of all substrings of w. What is the minimum
number of states in a non-deterministic finite automaton that accepts L?
26. Which one of the following languages over the alphabet {0,1} is described by the regular expression:
(0+1)*0(0+1)*0(0+1)*?
27. Given below are two finite state automata (→ indicates the start state and F indicates a final state)Which
of the following represents the product automaton Z×Y?
28. Match the following NFAs with the regular expressions they correspond to
1. ϵ + 0(01*1 + 00) * 01*
2. ϵ + 0(10 *1 + 00) * 0
3. ϵ + 0(10 *1 + 10) *1
4. ϵ + 0(10 *1 + 10) *10 *
29. Which of the following are regular sets?
30. Which of the following languages is regular?
31. Consider the following Finite State Automaton. The language accepted by this automaton is given by the
regular expression
32. Which one of the following is TRUE?
33. Which of the regular expressions given below represent the following DFA?
I) 0*1(1+00*1)*
II) 0*1*1+11*0*1
III) (0+1)*1
34. Which one of the following is CORRECT?
35. Which one of the following is TRUE?
Let L1 = {w ∈ {0,1}∗ | w has at least as many occurrences
of (110)’s as (011)’s}.
Let L2 = { ∈ {0,1}∗ | w has at least as many occurrences
of (000)’s as (111)’s}.
36. The length of the shortest string NOT in the language (over Σ = {a, b}) of the following regular
expression is ______________.
a*b*(ba)*a*
37. If s is a string over (0 + 1)* then let n0(s) denote the number of 0’s in s and n1(s) the number of 1’s in s.
Which one of the following languages is not regular?7" />
38. The following finite state machine accepts all those binary strings in which the number of 1's and 0's are
respectively.
39. Consider the following deterministic finite state automaton M. Let S denote the set of seven bit binary
strings in which the first, the fourth, and the last bits are 1. The number of strings in S that are accepted
by M is
40. Consider the following two statements:
A} Only S1 is correct
B} Only S2 is correct
C} Both S1 and S2 are correct
D} None of S1 and S2 is correct
41. Consider a DFA over ∑ = {a, b} accepting all strings which have number of a’s divisible by 6 and
number of b’s divisible by 8. What is the minimum number of states that the DFA will have?
A} 8
B} 14
C} 15
D} 48
42. Which one of the following strings is not a member of L (M)?
A} aaa
B} aabab
C} baaba
D} bab
43. Consider the non-deterministic finite automaton (NFA) shown in the figure.
State X is the starting state of the automaton. Let the language accepted by the NFA with Y as the only
accepting state be L1. Similarly, let the language accepted by the NFA with Z as the only accepting state
be L2. Which of the following statements about L1 and L2 is TRUE?
44. Consider the context-free grammar E → E + E E → (E * E) E → id
where E is the starting symbol, the set of terminals is {id, (,+,),*}, and the set of non-terminals is {E}.
For the terminal string id + id + id + id, how many parse trees are possible?
45. Which one of the following regular expressions represents the language: the set of all binary strings
having two consecutive 0s and two consecutive 1s?
46. The number of states in the minimum sized DFA that accepts the language defined by the regular
expression (0+1)*(0+1)(0+1)* is __________________ [Note that this question was originally asked as
Fill-in-the-Blanks type]
47. In the automaton below, s is the start state and t is the only final state.
Consider the strings u = abbaba, v = bab, and w = aabb. Which of the following statements is true?
48. In the context-free grammar below, S is the start symbol, a and b are terminals, and ϵ denotes the empty
string S → aSa | bSb | a | b | ϵ Which of the following strings is NOT generated by the grammar?
49. Which of the following languages is (are) non-regular? L1 = {0m1n | 0 ≤ m ≤ n ≤ 10000} L2 = {w | w
reads the same forward and backward} L3 = {w ∊ {0, 1} * | w contains an even number of 0's and an
even number of 1's}
50. If the regular set 'A' is represented by A= (01+1)* and the regular set 'B' is represented by B= ((01)*
1*)*, which of the following is true? (i) A ⊂ B (ii) B ⊂ A (iii) A and B are incomparable (iv) A = B
51. Construct a finite state machine with minimum number of states, accepting all strings over {a, b} such
that the number of a's is divisible by two and the number of b's is divisible by three.
52. Let L ⊆ ∑* where ∑ = {a, b}. Which of the following is true? a). L= { x | x has an equal number of a's
and b's } is regular b). L= { anbn | n ≥ 1 } is regular c). L= { x | x has more a's than b's } is regular d). L=
{ambn | m ≥ 1, n ≥ 1 } is regular
53. Following is a state table for some finite state machine.
A). Find the equivalence partition on the states of the machine.
B). Give the state table for the minimal machine. (Use appropriate names for the equivalent states. For
example if states X and Y are equivalent then use XY as the name for the equivalent state in the minimal
machine.)
54. Consider the given figure of state table for a sequential machine.
The number of states in the minimized machine will be
A} 1
B} 2
C} 3
D} 4
55. Let L1, L2 be any two context-free languages and R be any regular language. Then which of the
following is/are False? (I) (L1)'∪L2∪L1 is context-free (II) R'∪L2 is context-free (III) R∩L1∩L2 is
context-free (IV) R∩L2 is context-free
56. Given below are the transition diagrams for two finite state machine M1 and M2recognizing languages
L1 and L2 respectively.
a) Display the transition diagram for a machine that recognizes L1 . L2, obtained from transition diagrams
for M1 and M2 by adding only ε transitions and no new states. b) Modify the transition diagram obtained in
part(a) obtain a transition diagram for a machine that recognizes (L1.L2)∗ by adding only εtransitions and
no new states. (Final states are enclosed in double circles).
57. Consider the Deterministic Finite Automaton for input alphabets Σ = {a, b} L
=
The number of final state(s) will be
A} 1
B} 3
C} 2
D} 6
58. The finite state machine given in figure below recognizes:
A} any string of odd number of a’s
B} any string of odd number of b’s
C} any string of even number of a’s and odd number of b’s
D} any string of odd number of a’s and odd number of b’s
59. There is a colony of 8 cells arranged in a straight line where each day every cell competes with its
adjacent cells(neighbour). Each day, for each cell, if its neighbours are both active or both inactive, the
cell becomes inactive the next day, otherwise it becomes active the next day.
60. Print the pattern If input is 5
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15