You are on page 1of 6

Q.

Expression Evaluation
Evaluate an expression represented by a String. Expression can contain parentheses;
you can assume parentheses are well-matched. For simplicity, assume only binary
operations allowed are +, -, *, and /. Arithmetic Expressions can be written in one of
three forms:
Infix Notation: Operators are written between the operands they operate on, e.g. 3
+ 4.
Prefix Notation: Operators are written before the operands, e.g. +
34
Postfix Notation: Operators are written after operands, e.g. 3 4 +

Infix Expressions are harder for Computers to evaluate because of the additional
work needed to decide precedence. Infix notation is how expressions are written and
recognized by humans and, generally, input to programs. Given that they are harder
to evaluate, they are generally converted to one of the two remaining forms. A very
well-known algorithm for converting an infix notation to a postfix notation is
Shunting Yard Algorithm. This algorithm takes as input an Infix Expression and
produces a queue that has this expression converted to a postfix notation. Same
algorithm can be modified so that it outputs result of evaluation of expression instead
of a queue. Trick is using two stacks instead of one, one for operands and one for
operators.
Time Complexity: O(n)
Space Complexity: O(n)

Q. Arithmetic Expression Evaluation


The stack organization is very effective in evaluating arithmetic expressions.
Expressions are usually represented in Infix notation, in which each operator is
written between two operands (i.e., A + B). With this notation, we must distinguish
between (A + B) *C and A + (B * C) by using either parentheses or some operator
precedence convention. Thus, the order of operators and operands in an arithmetic
expression does not uniquely determine the order in which the operations are to be
performed.

Q. Differentiate between Polish and Reverse Polish Notation


Polish notation (prefix notation)
It refers to the notation in which the operator is placed before its two operands. Here
no parentheses are required, i.e., +AB
Reverse Polish notation (postfix notation)
It refers to the analogous notation in which the operator is placed after its two
operands. Again, no parentheses are required in Reverse Polish notation, i.e., AB+.
Stack organized computers are better suited for post-fix notation then the traditional
infix notation. Thus, the infix notation must be converted to the post-fix notation.
The conversion from infix notation to post-fix notation must take into consideration
the operational hierarchy.
There are 3 levels of precedence for 5 binary operators as given below:
Highest: Exponentiation (^)
Next highest: Multiplication (*) and division (/)
Lowest: Addition (+) and Subtraction (-)

Q. Stacks

Stack is a linear data structure which follows a particular order in which the
operations are performed. The order may be LIFO (Last in First Out) or FILO (First
in Last Out).

Mainly the following three basic operations are performed in the stack:
 Push: Adds an item in the stack. If the stack is full, then it is said to be an
Overflow condition.
 Pop: Removes an item from the stack. The items are popped in the reversed
order in which they are pushed. If the stack is empty, then it is said to be an
Underflow condition.
 Peek or Top: Returns top element of stack.
 is Empty: Returns true if stack is empty, else false.

Applications of stack:
 Balancing of symbols
 Infix to Postfix /Prefix conversion
 Redo-undo features at many places like editors, photoshop.
 Forward and backward feature in web browsers

Q. Doubly Linked List


A Doubly Linked List (DLL) contains an extra pointer, typically called previous
pointer, together with next pointer and data which are there in singly linked list.
Advantages
 A DLL can be traversed in both forward and backward direction.
 The delete operation in DLL is more efficient if pointer to the node to be
deleted is given.
 We can quickly insert a new node before a given node.
 In singly linked list, to delete a node, pointer to the previous node is needed.
To get this previous node, sometimes the list is traversed. In DLL, we can get
the previous node using previous pointer.
Disadvantages
 Every node of DLL Require extra space for a previous pointer. It is possible
to implement DLL with single pointer though (See this and this).
 All operations require an extra pointer previous to be maintained. For
example, in insertion, we need to modify previous pointers together with next
pointers. For example, in following functions for insertions at different
positions, we need 1 or 2 extra steps to set previous pointer.
Insertion
A node can be added in four ways
 At the front of the DLL
 After a given node.
 At the end of the DLL
 Before a given node.

Q. Difference b\w Stack and Queue


Q. Difference b\w Queue and Circular Queue

Q. B-Tree
B-Tree is a self-balancing search tree. When the number of keys is high, the data is
read from disk in the form of blocks. Disk access time is very high compared to main
memory access time.
The main idea of using B-Trees is to reduce the number of disk accesses. Most of
the tree operations (search, insert, delete, max, min, etc.) require O(h) disk accesses
where h is the height of the tree.
Properties of B-Tree
 All leaves are at same level.
 A B-Tree is defined by the term minimum degree ‘t’. The value of t depends
upon disk block size.
 Every node except root must contain at least t-1 keys. Root may contain
minimum 1 key.
 All nodes (including root) may contain at most 2t – 1 key.
 Number of children of a node is equal to the number of keys in it plus 1.
 All keys of a node are sorted in increasing order. The child between two keys
k1 and k2 contains all keys in the range from k1 and k2.
 B-Tree grows and shrinks from the root which is unlike Binary Search Tree.
Binary Search Trees grow downward and also shrink from downward.
 Like other balanced Binary Search Trees, time complexity to search, insert
and delete is O(LogN).
Following is an example B-Tree of minimum degree 3. Note that in practical B-
Trees, the value of minimum degree is much more than 3.

You might also like