Interview- Swamping two variables

This is a simple question that how to swap two values let say a and without using third variable. But this question gets confusing when you are asked to swap negative and positive value together.
Let say a=2
b=-3
Now swap a value into b and b value into a.

So the code will be:-

{
int a;
int b;
a=2;
b=-3;
a=b-a;
b=-(a)+b;
a=a+b;
Console.WriteLine(“a=”+a);

Console.WriteLine(“b=”+b);
}

a=-3
b=2

The good part about this code is that you can enter any value in a or b whether negative or positive it will swap it with ease.

Interview Question

Q1). char,varchar,nvarchar difference?

Char means every entry  in a row will take 8000 character space.
whether you store 5 letter string or 1000 letters string.

Varchar means you can specify during the schema design that how much space you want the column should  use for each entry.
e.g if you specify username varchar(10) then for each entry in a row the 10 character space will be use.

nvarchar means you can not only specify the space in the column but also additional asci characters are also included which can be entered in that column.

Q2)What are Triggers?
Trigger are special type of store procedures which can be set with the table so that for each update,insert or delete in the table. A query is run which saves the detail of operation done on table along with the data.
For example if we set update trigger on ITEM table then for each update of ITEM table a trigger will be fired which will save updated item record and time  in another table to maintain log of updates.
Trigger are of 3 type Onupdate, OnInsert, OnDelete.

Q3). Primary Key Vs Unique Key?

Primary key can not have null value unique key can have one null value.
Primary key automatically generates non clustered index where as unique key does not generate index automatically .

 

Interview-Example of Model View Controller In Real World

This is another simple question which was asked to me in an interview.
As all interviewer requires real world example of every concept so the MVC (Model View Controller) application can be seen in a Calculator.

The buttons is the Controller.-> when you press the add button request is send to model.
The back end hidden knowledge is Model-> When you press the add button some business logic runs let say two number are added in the calculator then the answer is send to view.

The Display is View->The View is responsible to display the result.

mvcdiagram

Interview-How To Store Directory Structure In Database

This was asked in an interview question that what will be the data structure schema of database if you  are given to save folder and they have files stored in it and also they contain sub folders in it.

So the best schema for this type of problem is that we will only require one table.
Yes one Table will do the job and more interestingly only 3 fields are enough to save  this information.

Field Name      –   Key                    – Data Type – Nullable

1). Dir                 – Primary Key   -int                   – Not Null
2). Dir_Parent –  Foreign Key   -int                   – Null Allowed
3). Description – Null                 -Varchar          – Not Null

dirdbschema

Here on top is the description of how data will be save in database using one table and .