Linked Lists

Q1. How will you that a circular linked list has Loop.

A1. This is a very common question, take two pointers say p and q, p = q-1, and then traverse the list, if at any point during traversing p=q, then that is the Loop.

Q2. Given a linked list with n nodes where n is larger number and Even. How will you divide the linked list into 2 equal parts in one traverse.

A2. Take 2 pointers p and q, what we know is that for last node -> next = null, we are looking at the following equation, say when p = n (end node) , then q = n/2, which gives us p=2q, which means that move pointer p twice pointer q, that is shift pointer p by 2 nodes and pointer q by 1 node, that way when p = n, q would be n/2 and we have achieved the result.

HINT - hint for the above question is "how to find the (n-1)th node in the linked list.

SQL

Q1. Given a database, with no choice of auto_increments, how will you select the next identity?

A2. The Answer varies to what database you are using.

a)MySql - use the function "last_insert_id()" , your query should say "select last_insert_id() As NewID from table".

b) Sql Server - User @@Identity, your query would be "select @@Identity As NewID from table".

c)Oracle - use SEQUENCES, first you need to create a sequence "create sequence newId startwith 1 increment by 1" then use "newId.currentval" and "newId.nextval" to get the current and next values.

## No comments:

Post a Comment