Test 2 – Important questions and answers
5 marks
1. Explain stack and queue operations
Stack
A stack is linear list of elements where insertion and deletion can be done only at one end
called the top of the stack (TOP).
Due to this property it is also called as last in first out type of data structure (LIFO).
Examples:
Basic Operations of stack are
1.push() − Pushing (storing) an element on the stack.
2.pop() − Removing (accessing) an element from the stack.
3.peek() − get the top data element of the stack, without removing it.
4.isFull() − check if stack is full.
5.isEmpty() − check if stack is empty.
Push Operation The process of putting a new data element onto stack is known as a Push
Operation.
Push operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.
Pop Operation in Stack
It helps in removing elements from the top of the stack. The steps involved in the pop operation
are:
1. The first step is to check whether the stack is empty or not.
2. If the stack is already empty, we cannot remove any element from it. So, we will return an
underflow error.
3. If the stack is not empty, remove the topmost element of the stack.
4. Decrement the position of ‘top’ by 1 and return.
The following example explains the pop operation:
Queue in Data Structure
The queue data structure is linear. It follows the FIFO approach i.e. First In First Out or Last In
Last Out. In the queue, the order of insertion is the same as the order of deletion of elements
because of the FIFO approach.
Eg. Queue waiting in a ticket counter
Queues are mostly used whenever there is a single resource and multiple users want to use that
resource. For example, in threads and CPU scheduling algorithms. A data structure queue is
analogous to a queue in real life. For example, people waiting in a queue to buy movie tickets.
Basic operations on Linear Queue:
The basic operations that we can perform on a queue are:
a. enqueue(): To insert an element into the queue.
b. dequeue(): To delete an element from a queue.
c. peek(): To print/display the element at the front without deleting it.
d. isfull(): To check if the queue is full or not.
e. isempty(): To check if the queue is empty or not.
a)enqueue() Operation in Queue
The queue has two ends pointed by Front and Rear. The enqueue() operation is always
performed at the Rear end.
While performing enqueue() operation, we always check whether the queue is full or not. In case
the queue is full, we return an overflow error. Otherwise, we insert the element at the rear end
and make the Rear point to the next position in the queue.
For example, suppose we have a queue with elements A, B, C, D and we wish to insert E into the
queue. We can insert this new element into the queue as follows:
b)dequeue() Operation
This operation is used to delete an element from the queue. dequeue() operation is always
performed at the ‘Front’ of the queue.
While performing dequeue() operation, we always make sure that the queue is not already empty.
If the queue is already empty, there is nothing to delete. In that case, we return an underflow
error. If the queue is not empty, we delete the front node and move the front pointer to the next
node.
The following diagram depicts the deletion operation:
Advantages of queues:
The advantages of queues are that the multiple data can be handled, and they are fast and
flexibility.
Disadvantages of queues:
To include a new element in the queue, the other elements must be deleted.
Implementation:
Queue can be implemented using array or linked list.
Applications of Queue:
1. Task Scheduling
2. Resource allocation
3. Traffic management
4. Operating Systems.
2. Given a binary tree. Insert the element 40, 550 and 90. Delete the element 20 and write
down the rules for deletion - Dr. Lissy D
After insertion of node 40
After insertion of node 550
After insertion of node 90
Binary tree insertion
1. Read the search element to be inserted.
2. Conduct binary search to check whether the element is already present in the tree or not.
3. If the element is already present, discard insertion.
4. If the element is not there, stop the searching process once a node data greater than the
search data is reached.
5. Create a new node with the data part of the node as the search element and left and right
pointers set to nil.
6. If there exist a root in the binary tree, then check whether the previous node of the node
where searching stopped > new nodes’ data, if yes, insert the new node as previous nodes’ left
node. If no, insert the new node as previous nodes’ right node.
7. If there exists no root (empty tree), make the new node as the root node.
Rules for deletion of node from binary tree
When deleting a node from a binary search tree (BST), consider the number of children the node
has and follow these steps:
· Leaf node
If the node has no children, you can remove it directly and free its memory.
· Node with one child
If the node has one child, you can connect the parent directly to the child and free the node.
· Node with two children
If the node has two children, you can find the inorder successor (the smallest node in the right
subtree or the largest node in the left subtree) and replace the node to be removed with the
inorder successor. Then, you can delete the inorder successor.
After deletion of node 20
________________________________________________________________________
3. Give a brief discussion on tower of hanoi
Design and implement a recursive algorithm to solve the Towers of Hanoi problem for one or more
disks.
Observations:
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple
rules:
• Only one disk can be moved at a time.
• Each move consists of taking the upper disk from one of the stacks and placing it on top of
another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
• Always a disk must be placed over a disk that is wider than it.
Fig: Pictorial representation of arranging three disks
Towers of Hanoi problem is solved by the process of recursion
Recursion is the process of a function or procedure calling itself repeatedly until it meets a base
condition.
Eg., factorial of a number, tower of hanoi etc.
Base condition: It is a condition that stops a function from calling itself indefinitely and returns a
result.
For eg., In a factorial algorithm, the base case is n=1. ie., Factorial(1)=1.
Towers of Hanoi is solved by recursion because solving the problem with 5 disks includes solving
problem with 4 disks. Solving the problem with 4 disks includes solving the problem with 3 disks
and so on.
ALGORITHM:
Procedure: towershanoi(ndisks, originpole, sparepole, finalpole)
1. Take a variable ndisks to indicate the number of disks. If n > 1, go to step 2, otherwise go
to step 5.
2. Transfer all disks from originpole to finalpole except bottom disk by recursively calling
the procedure towershanoi(ndisks - 1, originpole, sparepole, finalpole)
3. Transfer the bottom disk from originpole to finalpole
4. Transfer all disks from sparepole to finalpole except bottom disk by recursively calling the
procedure towershanoi(ndisks - 1, sparepole, originpole, finalpole)
5. If ndisks = 1, then transfer disk from originpole to finalpole
___________________________________________________________________________
4. Discuss about linked list
● A linked list is a linear data structure which can store a collection of "nodes" connected
together via links i.e. pointers.
● Linked lists nodes are not stored at a contiguous location, rather they are linked using
pointers to the different memory locations.
● A node consists of the data value and a pointer to the address of the next node within the
linked list.
● A linked list is a dynamic linear data structure whose memory size can be allocated or de-
allocated at run time based on the operation insertion or deletion, this helps in using system
memory efficiently.
● Linked lists can be used to implement various data structures like a stack, queue, graph,
hash maps, etc.
1. Data: The actual value to be stored.
2. Pointer (or next): A link to the next node in the sequence.
A linked list starts with a head node which points to the first node. Every node consists of data
which holds the actual data (value) associated with the node and a next pointer which holds the
memory address of the next node in the linked list. The last node is called the tail node in the list
which points to null indicating the end of the list.
Basic Operations on Linked Lists
1. Insertion: Adding a new node to the list.
○ At the beginning
○ At the end
○ At a specific position
2. Deletion: Removing an existing node from the list.
○ From the beginning
○ From the end
○ From a specific position
3. Traversal: Visiting each node in the list to perform some operation, such as printing the
elements.
4. Search: Finding a node with a specific value.
Advantage:
● Dynamic Data Structure - Ability to shrink and grow at the runtime by de-allocating
or allocating memory. So, there is no need for initial size.
● No Memory Wastage - Only the required memory is allocated.
● Insertion and Deletion Operation- Only the address present in the pointers needs to be
updated.
Disadvantage:
● Memory Usage - linked list is more than the memory required by an array, as a pointer to
store the address of the next element is required.
● Traversal - Traversing a linked list is a bit more time-consuming when compared to an
array.
● Reverse Traversal - In a singly linked list, reverse traversal is not possible, as every node
stores only the address of the next node. In the case of a doubly-linked list, reverse traversal
is possible, but it consumes more memory.
● Random Access - Random access is not possible in a Linked List.
5. Write an algorithm for Array order reversal
Rearrange the elements in the array so that they appear in reverse order.
Example Input:
1 2 3 4 5 6 7
Output:
7 6 5 4 3 2 1
Observations:
Consider an array ‘a’ of size n.
The first element in the array should become last element, the second element should become the
last but on element and so on.
In the above diagram, the array size =7 i.e., n=7
So the steps are:
a[1] exchanged with a[7]
a[2] exchanged with a[6]
a[3] exchanged with a[5]
a[4] exchanged with a[4] - actually there is no exchange here.
Only take the array suffixes:
1 7
2 6
3 5
4 4
In setting up the algorithm, we need a pair of suffixes that model this increasing-decreasing
behavior.
The increasing suffix can be a variable ‘i’ which is simply incremented (increased) by 1with each
step.
The decreasing suffix can be successfully achieved by n-i+1
1 7
2 6
3 5
4 4
i n-i+1
ie., a[i]a[n-i+1]
Now apply the swapping algorithm:
Let ‘t’ be the temporary variable used for swapping.
t = a[i]
a[i] = a[n-i+1]
a[n-i+1] = t
ALGORITHM
START
Step 1 → Take an array variable ‘a’ of size ‘n’
Step 2 → Store the numbers as continuous values in a
Step 3 → Create a variable say ‘t’ for storing temporary value. Let t=0
Step 4 → Create a variable say ‘i’ for iterating through the array elements. Let i=1
Step 5 → While there are still pairs of array elements to be exchanged, do the following:
i) Store value at a[i] to t
ii) Store value at a[n-i+1] to a[i]
iii) Store t value to a[n-i+1]
iv) increment the value of i by 1 i.e., i = i+1
STOP
6. Explain basic and repetitive rules of pattern recognition with examples
A pattern defines a set of properties that some strings will possess and other strings will not.
Eg.,
The string literal “123-45-6789”, for example, follows the pattern that all Social Security
numbers must follow. The pattern of Social Security numbers is understood to be any 3 digits
followed by a dash (-) followed by any 2 digits followed by a dash followed by any 4 digits.
In computer programming, patterns are known as regular expressions.
A regular expression defines a pattern such that a particular string will either match the pattern or
will not match the pattern. Regular expressions are extremely powerful techniques for processing
textual data; particularly for finding elements in large textual databases.
How to Write a Pattern or Basic Rules for Writing a Pattern
1) Any single character (except for a few special characters) is a pattern. The character
represents itself.
2) The period symbol (.) is a pattern. The character represents any single character. The
period is special.
3) If A and B are both patterns, then so are
a) AB : Sequencing represents the pattern A followed by the pattern B
b) A|B : Alternation represents either the pattern A or the pattern B
c) (A) : Grouping sets aside pattern A as a group. The parentheses are special
Can you write a pattern for Credit Card Number?
(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)-
(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)-
(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)-
(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)
Repetition Rules
4) If A is a pattern then so are
a) A* : Denotes zero or more repetitions of A. The * is special.
b) A+ : Denotes one or more repetitions of A. The + is special.
c) A? : Denotes zero or one occurrence of A. The ? is special.
d) A{m} : Denotes exactly m repetitions of A. The curly brackets are special.
e) A{m,n}: Denotes as least m and no more than n repetitions of A.
f) A{m, } : Denotes at least m repetitions of A
0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)-
(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)
(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)
(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)
(0|1|2|3|4|5|6|7|8|9){4}- (0|1|2|3|4|5|6|7|8|9) {4}- (0|1|2|3|4|5|6|7|8|9) {4}-(0|1|2|3|4|5|6|7|8|9)
{4}
________________________________________________________________________
7. Explain Software Design
SOFTWARE DESIGN:
• By breaking down a Project into major Functions or related Software Engineering
Activities.
• Decompose the software into functions that can each be estimated individually.
Top down design:
• One design technique used by the software developers is called Top down design.
• Top down design starts with the summary of the problem and proceeds by successively
refining vague instructions into more explicit instructions.
• A top-level design is a refinement (decomposition) of the problem into an algorithm
of a few instructions.
• These top-level instructions are necessarily extremely vague. For the off-road vehicle
problem a suitable top-level design would be something like five-instruction algorithm:
• Example : Draw a image of an off-road vehicle (Refer Decomposition topic).
Successive prototyping:
• Another form of software design used by software engineers today is known as
Successive prototyping.
• A prototype of an any object is an early approximation of the object.
• A prototype for a computer program is a partially functioning piece of software.
Typically, a prototype performs some, but not all, of the requirements from the problem
definition.
Example: Automobile manufacturers often create clay or plastic models of a potential new
cars long before they actually intend to manufacture them.
Designing with successive prototyping:
Designing with successive prototyping consists of a sequence of individual prototypes that
progressively lead to a final working program. The early prototypes provide very little of the
desired functionality, while later prototypes are nearly finished programs.
The design for a new car might begin with a sketch as a first prototype, then proceed to a
clay model for a second prototype, followed by concept cars serving as later prototypes.
Example: successive prototyping of Sal’s Surfboard online Shop (SSS) software
A reasonable succession of prototypes of the above software is described below:
Prototype 1—Initial web page only (see the following Figure)
FIGURE: Initial page for SSS online purchase website
Prototype 2—Same as Prototype 1 except incorporating Sal’s requested improvements
Prototype 3—Same as Prototype 2 except including all inventory pages (no ability to
modify cart)
Prototype 4—Same as Prototype 3 except including ability to add items and view cart
Prototype 5—Same as Prototype 4 except including secure checkout
functionality
Prototype 6—Same as Prototype 5 except supporting site search functionality and “Contact
us” page.
In this case the last prototype (Prototype 6) may provide all required functionality; this
prototype could be ready to submit as a final product after sufficient software testing.
Successive prototyping is a form of decomposition, because each prototype represents a
focus on some portion of the entire set of requirements.
This means that the software engineer has to decompose the entire problem into parts and
implement the parts successively.
______________________________________________________________________________
8. Explain decomposition with example
Decomposition helps by breaking down complex problems into more manageable parts.
Decomposition is one of the four cornerstones of Computer Science.
It involves breaking down a complex problem or system into smaller parts that are more
manageable and easier to understand.
The smaller parts can then be examined and solved, or designed individually, as they are simpler
to work with.
FIGURE: Divide and conquer means to separate a problem into sub problems.
Example 1: The problem of making pizza
The larger problem of making a pizza can be divided into collection of six sub problems:
FIGURE: Decomposing the problem of making a pizza.
Decomposing a problem using outlining technique:
Decomposition is frequently taught as a technique for writing, known as outlining. The
basic notion of outlining is to organize a work beginning by decomposing the entire work into its
main ideas. Each of the main tasks might be divided into sub points, like dividing problems into
sub problems. Outlining can continue by decomposing any appropriate sub point into its own sub
points.
The standard notation for outlining uses Roman numerals to index the main tasks, capital
letters for the first-level sub points, numbers for second-level sub points, small letters for third-
level sub points, and so forth.
FIGURE: Standard outline form.
Example 2: Design an algorithm to draw the image of an off-road vehicle shown in
following figure.
One design technique used by software developers is called top-down design. A top-level
design is a refinement (decomposition) of the problem into an algorithm of a few instructions.
1. Draw a green grille.
2. Draw bumper just below the grille.
3. Draw the tires behind the grille and bumper.
4. Draw the windshield just above the grille.
5. Draw two auxiliary lights on top of the windshield
FIGURE: Top-level algorithm for the off-road vehicle drawing.
1. Draw a green grille.
1.1. Draw a green grille background.
1.2. Draw the left headlight.
1.3. Draw four equally spaced black vertical rectangles for grille openings.
1.4. Draw the right headlight.
2. Draw bumper just below the grille.
3. Draw the tires behind the grille and bumper.
3.1. Draw the left tire (a black rectangle) slightly protruding left of the grille and bumper.
3.2. Draw a half-moon hubcap extending outside the left tire.
3.3. Draw the right tire (a black rectangle) slightly protruding right of the grille and bumper.
3.4. Draw a half-moon hubcap extending outside the right tire.
4. Draw the windshield just above the grille.
4.1. Draw a gray rectangle for the outside of the windshield.
4.2. Draw a white rectangle for the center of the windshield.
5. Draw two auxiliary lights on top of the windshield.
5.1. Draw the left auxiliary light.
5.2. Draw the right auxiliary light.
FIGURE: Second-level algorithm for the off-road vehicle drawing.
__________________________________________________________________________
9. Write an algorithm for base conversion
• Convert a decimal integer to its corresponding octal representation
Algorithm Logic
Variables:
1. r-remainder
2. newbase 8
3. q- quotient initially the given number i.e., q 93
4. newrep - array variable to store answer
5. ndigit 0 – to traverse the newrep array
Repeat
r q%newbase
r ascii(r)
ndigit ndigit+1
newrep[ndigit] r
q q/newbase
Until q 0
Repeat
print newrep[ndigit]
ndigitndigit-1
Until ndigit1
Step Q R Ndigit
Initial 93 0
Iteration 1 93/8=11 93%8=5 0+1=1
Iteration 2 11/8=1 11%8=3 1+1=2
Iteration 3 1/8=0 1%8=1 2+1=3
OUTPUT: newrep[ ] array
0 1 2 3
5 3 1
_____________________________________________________________________________
2 marks
1. Differentiate between linear and non-linear data structures
It is a type of Data Structure where the data elements get linearly or sequentially arranged. The
elements attach themselves to their next and previous adjacents. The structure involves only a
single level- allowing a user to traverse all its components in a single run.
The linear data structure is very easy to understand and implement due to its linear arrangement,
for example, stack, array, linked list, queue, etc.
Non-Linear Data Structure
It is a form of data structure where the data elements don’t stay arranged linearly or sequentially.
Since the data structure is non-linear, it does not involve a single level. Therefore, a user can’t
traverse all of its elements in a single run.
The non-linear data structure is not very easy to implement as compared to the linear data
structure. The utilization of computer memory is more efficient in this case, for example, graphs
and trees.
______________________________________________________________________________
2.Complete the number hive puzzle
______________________________________________________________________________
3.Define data structures
Data structures is a way of organizing and storing the data.
________________________________________________________________________
4.Find the indegree and outdegree of node named 3.
For directed graphs, we have indegree and outdegree. The indegree of a node is the
number of incoming edges. The outdegree of anode is the number of outgoing edges.
5.Define tree with a neat diagram
Tree data structure is a specialized data structure to store data in hierarchical manner. It is used
to organize and store data in the computer to be used more effectively. Tree data structure has
roots, branches, and leaves connected.
______________________________________________________________________________
6.Write the Applications of stack and queue
Applications of stack
● used in Reversal of String.
● Checking Validity of any Expression Containing Nested Parenthesis.
● Function Call.
● Conversion of Infix expression to postfix form.
● Evaluation of Postfix Expression.
● Backtracking.
Applications of Queue
Scheduling in operating system
Print spooling
Traffic Modeling
Message queuing
Simulations
Event Handling
7.Define recursion
Recursion is the process of a function or procedure calling itself repeatedly until it meets a base
condition.
Eg., factorial of a number, tower of hanoi etc.
Base condition: It is a condition that stops a function from calling itself indefinitely and returns a
result.
For eg., In a factorial algorithm, the base case is n=1. ie., Factorial(1)=1.
_____________________________________________________________________________
8.What is successive prototyping
It is a process of creating multiple prototypes of a system, based on user feedback, and refining
them until the final product is achieved.
_____________________________________________________________________________
9.Write the definition for functional and non-functional requirements
Functional requirements
To specify the particular task(s) the software must perform
Nonfunctional requirements
To define other characteristics and constraints related to the software. Nonfunctional which
includes reliability, safety, security, performance, delivery, and help facilities. These non-
functional requirements are useful.
A good list of functional requirements must be
• Clear
• Consistent
• Complete
______________________________________________________________________________
10.Define cause and effect relationship.
Cause and effect relationship consist of logical condition (the cause ) that forces the program to
perform some task (the effect)
The ability to apply general rules to particular situations is known as deductive reasoning.
_____________________________________________________________________________