zencode Solution Pvt Ltd ( call based 2016, 2017)
drive date 11-5-2018
Address: III & IV Floors, NCR Complex 580/B, Sector 6, HSR layout Landmark – Next to Bank Of India. Bengalaru – 560102
Array is a data structure used to store homogeneous elements at contiguous locations. Size of an array must be provided before storing data.
Let size of array be n.
Accessing Time: O(1) [This is possible because elements
are stored at contiguous locations]
Search Time: O(n) for Sequential Search:
O(log n) for Binary Search [If Array is sorted]
Insertion Time: O(n) [The worst case occurs when insertion
happens at the Beginning of an array and
requires shifting all of the elements]
Deletion Time: O(n) [The worst case occurs when deletion
happens at the Beginning of an array and
requires shifting all of the elements]
Example : For example, let us say, we want to store marks of all students in a class, we can use an array to store them. This helps in reducing the use of number of variables as we don’t need to create a separate variable for marks of every subject. All marks can be accessed by simply traversing the array.
A linked list is a linear data structure (like arrays) where each element is a separate object. Each element (that is node) of a list is comprising of two items – the data and a reference to the next node.
Types of Linked List :
1. Singly Linked List : In this type of linked list, every node stores address or reference of next node in list and the last node has next address or reference as NULL. For example 1->2->3->4->NULL
2. Doubly Linked List : In this type of Linked list, there are two references associated with each node, One of the reference points to the next node and one to the previous node. Advantage of this data structure is that we can traverse in both the directions and for deletion we don’t need to have explicit access to previous node. Eg. NULL<-1<->2<->3->NULL
3. Circular Linked List : Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or doubly circular linked list. Advantage of this data structure is that any node can be made as starting node. This is useful in implementation of circular queue in linked list. Eg. 1->2->3->1 [The next pointer of last node is pointing to the first]
Accessing time of an element : O(n)
Search time of an element : O(n)
Insertion of an Element : O(1) [If we are at the position
where we have to insert
Deletion of an Element : O(1) [If we know address of node
previous the node to be
Example : Consider the previous example where we made an array of marks of student. Now if a new subject is added in the course, its marks also to be added in the array of marks. But the size of the array was fixed and it is already full so it can not add any new element. If we make an array of a size lot more than the number of subjects it is possible that most of the array will remain empty. We reduce the space wastage Linked List is formed which adds a node only when a new element is introduced. Insertions and deletions also become easier with linked list.
One big drawback of linked list is, random access is not allowed. With arrays, we can access i’th element in O(1) time. In linked list, it takes Θ(i) time.
A stack or LIFO (last in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the last element that was added. In stack both the operations of push and pop takes place at the same end that is top of the stack. It can be implemented by using both array and linked list.
Insertion : O(1)
Deletion : O(1)
Access Time : O(n) [Worst Case]
Insertion and Deletion are allowed on one end.
Example : Stacks are used for maintaining function calls (the last called function must finish execution first), we can always remove recursion with the help of stacks. Stacks are also used in cases where we have to reverse a word, check for balanced parenthesis and in editors where the word you typed the last is the first to be removed when you use undo operation. Similarly, to implement back functionality in web browsers.
A queue or FIFO (first in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: enqueue, the process of adding an element to the collection.(The element is added from the rear side) and dequeue, the process of removing the first element that was added. (The element is removed from the front side). It can be implemented by using both array and linked list.
Insertion : O(1)
Deletion : O(1)
Access Time : O(n) [Worst Case]
Example : Queue as the name says is the data structure built according to the queues of bus stop or train where the person who is standing in the front of the queue(standing for the longest time) is the first one to get the ticket. So any situation where resources are shared among multiple users and served on first come first server basis. Examples include CPU scheduling, Disk Scheduling. Another application of queue is when data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.
Circular Queue The advantage of this data structure is that it reduces wastage of space in case of array implementation, As the insertion of the (n+1)’th element is done at the 0’th index if it is empty.
Commonly asked DBMS interview questions | Set 1
What are advantages of DBMS over traditional file based systems?
Ans: Database management systems were developed to handle the following difﬁculties of typical ﬁle-processing systems supported by conventional operating systems.
1. Data redundancy and inconsistency
2. Difﬁculty in accessing data
3. Data isolation – multiple ﬁles and formats
4. Integrity problems
5. Atomicity of updates
6. Concurrent access by multiple users
7. Security problems
What are super, primary, candidate and foreign keys?
Ans: A superkey is a set of attributes of a relation schema upon which all attributes of the schema are functionally dependent. No two rows can have the same value of super key attributes.
A Candidate key is minimal superkey, i.e., no proper subset of Candidate key attributes can be a superkey.
A Primary Key is one of the candidate keys. One of the candidate keys is selected as most important and becomes the primary key. There cannot be more that one primary keys in a table.
Foreign key is a field (or collection of fields) in one table that uniquely identifies a row of another table. See this for an example.
What is the difference between primary key and unique constraints?
Ans: Primary key cannot have NULL value, the unique constraints can have NULL values. There is only one primary key in a table, but there can be multiple unique constrains.
What is database normalization?
Ans: It is a process of analyzing the given relation schemas based on their functional dependencies and primary keys to achieve the following desirable properties:
1) Minimizing Redundancy
2) Minimizing the Insertion, Deletion, And Update Anomalies
Relation schemas that do not meet the properties are decomposed into smaller relation schemas that could meet desirable properties.
What is SQL?
SQL is Structured Query Language designed for inserting and modifying in a relational database system.
What are the differences between DDL, DML and DCL in SQL?
Ans: Following are some details of three.
DDL stands for Data Definition Language. SQL queries like CREATE, ALTER, DROP and RENAME come under this.
DML stands for Data Manipulation Language. SQL queries like SELECT, INSERT and UPDATE come under this.
DCL stands for Data Control Language. SQL queries like GRANT and REVOKE come under this.
What is the difference between having and where clause?
Ans: HAVING is used to specify a condition for a group or an aggregate function used in select statement. The WHERE clause selects before grouping. The HAVING clause selects rows after grouping. Unlike HAVING clause, the WHERE clause cannot contain aggregate functions. (See this for examples).
See Having vs Where Clause? for more details
How to print duplicate rows in a table?
Ans: See http://quiz.geeksforgeeks.org/how-to-print-duplicate-rows-in-a-table/
What is Join?
Ans: An SQL Join is used to combine data from two or more tables, based on a common field between them. For example, consider the following two tables.
ENROLLNO STUDENTNAME ADDRESS
1000 geek1 geeksquiz1
1001 geek2 geeksquiz2
1002 geek3 geeksquiz3
Following is join query that shows names of students enrolled in different courseIDs.
SELECT StudentCourse.CourseID, Student.StudentName
INNER JOIN Customers
ON StudentCourse.EnrollNo = Student.EnrollNo
ORDER BY StudentCourse.CourseID;
The above query would produce following result.
What is Identity?
Ans: Identity (or AutoNumber) is a column that automatically generates numeric values. A start and increment value can be set, but most DBA leave these at 1. A GUID column also generates numbers; the value of this cannot be controlled. Identity/GUID columns do not need to be indexed.
What is a view in SQL? How to create one
Ans: A view is a virtual table based on the result-set of an SQL statement. We can create using create view syntax.
CREATE VIEW view_name AS
What are the uses of view?
1. Views can represent a subset of the data contained in a table; consequently, a view can limit the degree of exposure of the underlying tables to the outer world: a given user may have permission to query the view, while denied access to the rest of the base table.
2. Views can join and simplify multiple tables into a single virtual table
3. Views can act as aggregated tables, where the database engine aggregates data (sum, average etc.) and presents the calculated results as part of the data
4. Views can hide the complexity of data; for example a view could appear as Sales2000 or Sales2001, transparently partitioning the actual underlying table
5. Views take very little space to store; the database contains only the definition of a view, not a copy of all the data which it presentsv.
6. Depending on the SQL engine used, views can provide extra security
Source: Wiki Page
What is a Trigger?
Ans: A Trigger is a code that associated with insert, update or delete operations. The code is executed automatically whenever the associated query is executed on a table. Triggers can be useful to maintain integrity in database.
What is a stored procedure?
Ans: A stored procedure is like a function that contains a set of operations compiled together. It contains a set of operations that are commonly used in an application to do some common database tasks.
What is the difference between Trigger and Stored Procedure?
Ans: Unlike Stored Procedures, Triggers cannot be called directly. They can only be associated with queries.
What is a transaction? What are ACID properties?
Ans: A Database Transaction is a set of database operations that must be treated as whole, means either all operations are executed or none of them.
An example can be bank transaction from one account to another account. Either both debit and credit operations must be executed or none of them.
ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties that guarantee that database transactions are processed reliably.
What are indexes?
Ans: A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and the use of more storage space to maintain the extra copy of data.
Data can be stored only in one order on disk. To support faster access according to different values, faster search like binary search for different values is desired, For this purpose, indexes are created on tables. These indexes need extra space on disk, but they allow faster search according to different frequently searched values.
What are clustered and non-clustered Indexes?
Ans: Clustered indexes is the index according to which data is physically stored on disk. Therefore, only one clustered index can be created on a given database table.
Non-clustered indexes don’t define physical ordering of data, but logical ordering. Typically, a tree is created whose leaf point to disk records. B-Tree or B+ tree are used for this purpos
Q. There is a table where only one row is fully repeated. Write a Query to find the Repeated row
In the above table, we can find duplicate row using below query.
SELECT name, section FROM tbl
GROUP BY name, section
HAVING COUNT(*) > 1
Q. Query to find 2nd highest salary of an employee?
SELECT max(salary) FROM EMPLOYEES WHERE salary IN
(SELECT salary FROM EMPLOYEEs MINUS SELECT max(salary)
SELECT max(salary) FROM EMPLOYEES WHERE
salary <> (SELECT max(salary) FROM EMPLOYEES);
Q.Consider the following Employee table. How many rows are there in the result of following query?
ID salary DeptName
1 10000 EC
2 40000 EC
3 30000 CS
4 40000 ME
5 50000 ME
6 60000 ME
7 70000 CS
How many rows are there in the result of following query?
FROM Employee E
WHERE EXISTS (SELECT E2.salary
FROM Employee E2
WHERE E2.DeptName = ‘CS’
AND E.salary > E2.salary)
Following 5 rows will be result of query as 3000 is the minimum salary of CS Employees and all these rows are greater than 30000.
Q. Write a trigger to update Emp table such that, If an updation is done in Dep table then salary of all employees of that department should be incremented by some amount (updation)
Assuming Table name are Dept and Emp, trigger can be written as –
CREATE OR REPLACE TRIGGER update_trig
AFTER UPDATE ON Dept
FOR EACH ROW
CURSOR emp_cur IS SELECT * FROM Emp;
FOR i IN emp_cur LOOP
IF i.dept_no = :NEW.dept_no THEN
DBMS_OUTPUT.PUT_LINE(i.emp_no); — for printing those
UPDATE Emp — emp number which are
SET sal = i.sal + 100 — updated
WHERE emp_no = i.emp_no;
Q. There is a table which contains two column Student and Marks, you need to find all the students, whose marks are greater than average marks i.e. list of above average students.
SELECT student, marks
WHERE marks > SELECT AVG(marks) from table;
Q.Name the student who has secured third highest marks using sub queries.
FROM Employee Emp1
WHERE 2 = (SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary
*LOGIC- Number of people with salary higher than this person will be 2.
Q. Why we cannot use WHERE clause with aggregate functions like HAVING ?
The difference between the having and where clause in SQL is that the where clause canNOT be used with aggregates, but the having clause can. Please note : It is not a predefined rule but by and large you’ll see that in a good number of the SQL queries, we use WHERE prior to GROUP BY and HAVING after GROUP BY.
The Where clause acts as a pre filter where as Having as a post filter.
The where clause works on row’s data, not on aggregated data.
Let us consider below table ‘Marks’.
Student Course Score
a c1 40
a c2 50
b c3 60
d c1 70
e c2 80
Consider the query
SELECT Student, sum(Score) AS total
This would select data row by row basis. The having clause works on aggregated data.
For example, output of below query
SELECT Student, sum(score) AS total FROM Marks
When we apply having in above query, we get
SELECT Student, sum(score) AS total
FROM Marks having total > 70
Q. Difference between primary key and unique key and why one should use unique key if it allows only one null ?
Only one in a row(tuple).
Never allows null value(only key field).
Unique key identifier and can not be null and must be unique.
Can be more than one unique key in one row.
Unique key can have null values(only single null is allowed).
It can be a candidate key.
Unique key can be null and may not be unique.
Q. What’s the difference between materialized and dynamic view?
Disk based and are updated periodically based upon the query definition.
A materialized table is created or updated infrequently and it must be synchronized with its associated base tables.
Virtual only and run the query definition each time they are accessed.
A dynamic view may be created every time that a specific view is requested by the user.
Q. What is embedded and dynamic SQL?
Static or Embedded SQL
SQL statements in an application that do not change at runtime and, therefore, can be hard-coded into the application.
SQL statements that are constructed at runtime; for example, the application may allow users to enter their own queries.
Dynamic SQL is a programming technique that enables you to buildSQL statements dynamically at runtime. You can create more general purpose, flexible applications by using dynamic SQL because the full text of a SQL statement may be unknown at compilation.
S.No. Static (embedded) SQL Dynamic (interactive) SQL
1. In static SQL how database will be accessed is predetermined in the embedded SQL statement. In dynamic SQL, how database will be accessed is determined at run time.
2. It is more swift and efficient. It is less swift and efficient.
3. SQL statements are compiled at compile time. SQL statements are compiled at run time.
4. Parsing, validation, optimization, and generation of application plan are done at compile time. Parsing, validation, optimization, and generation of application plan are done at run time.
5. It is generally used for situations where data is distributed uniformly. It is generally used for situations where data is distributed non-uniformly.
6. EXECUTE IMMEDIATE, EXECUTE and PREPARE statements are not used. EXECUTE IMMEDIATE, EXECUTE and PREPARE statements are used.
7. It is less flexible. It is more flexible.
Q. What is the difference between CHAR and VARCHAR?
CHAR and VARCHAR are differ in storage and retrieval.
CHAR column length is fixed while VARCHAR length is variable.
The maximum no. of character CHAR data type can hold is 255 character while VARCHAR can hold up to 4000 character.
CHAR is 50% faster than VARCHAR.
CHAR uses static memory allocation while VARCHAR uses dynamic memory allocation.