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 .

Stack Push Operation on Link List C#

Now i will be presenting the implementation  of stack on linked list.

Every body would be aware of coding stack on arrays which is a very simple step. But some people find difficulty while implementing the stack on link list.
First you should know what is a stack, Stack or LIFO Stack (last in first out) is a type of data structure which is use to put the values in a proper from and is implemented in various computer programs, It contains one entry and exit point from where values can push in or pop out of the Stack.
*/for more details on stack you can view stack description at (www.farazashraf.wordpress.com/stack)*/

Link List is also another form of data structure which contains item and it’s linking next node.
*/for more details on stack you can view stack description at (www.farazashraf.wordpress.com/linklist)*/

So i will implementing the stack push operation in link list.
It will be done through a C# class which i have named test_Stack.
It contains the private variable first which will be null.
A private inner class Node is used to declare string item and next node.
A bool method is just used to check if the first node is empty .

class test_Stack
    {
        private Node first = null;
        private class Node
        {
            public string item ;
            public Node next;
        }
       public bool check()
        {
            return first == null;
        }
       public void push(string item)
        {
            Node oldfirst = first;
            first = new Node();
            first.item = item;
            first.next = oldfirst;
        
        }
You can call the method in the main function to check if the class is working fine. By just creating an object and calling the method.

static void Main(string[] args)
        {
            test_Stack test1 = new test_Stack();
            string item=null;
            int i=2;
            while (i != 0)
            {
                Console.WriteLine(“enter the test value”);
                item = Console.ReadLine();
                test1.push(item);
                i–;
            }
       

Sql Script (demobld.sql)

This is the best script use by all learners in sql. Copy the below script and paste it on your notepad. Save the file as demobld.sql . Remember to change the file name to .sql and properties to all files while saving the notepad file.
Run the script in oracle sql 10g .

–Script starts here:


— Copyright (c) Oracle Corporation 1988, 2000.  All Rights Reserved.

— NAME
—   demobld.sql

— DESCRIPTION
—   This script creates the SQL*Plus demonstration tables in the
—   current schema.  It should be STARTed by each user wishing to
—   access the tables.  To remove the tables use the demodrop.sql
—   script.

—  USAGE
—    From within SQL*Plus, enter:
—        START demobld.sql

SET TERMOUT ON
PROMPT Building demonstration tables.  Please wait.

DROP TABLE EMP;
DROP TABLE DEPT;
DROP TABLE BONUS;
DROP TABLE SALGRADE;
DROP TABLE DUMMY;

CREATE TABLE EMP
       (EMPNO NUMBER(4) NOT NULL,
        ENAME VARCHAR2(10),
        JOB VARCHAR2(9),
        MGR NUMBER(4),
        HIREDATE DATE,
        SAL NUMBER(7, 2),
        COMM NUMBER(7, 2),
        DEPTNO NUMBER(2));

INSERT INTO EMP VALUES
        (7369, ‘SMITH’,  ‘CLERK’,     7902,
        TO_DATE(’17-DEC-1980′, ‘DD-MON-YYYY’),  800, NULL, 20);
INSERT INTO EMP VALUES
        (7499, ‘ALLEN’,  ‘SALESMAN’,  7698,
        TO_DATE(’20-FEB-1981′, ‘DD-MON-YYYY’), 1600,  300, 30);
INSERT INTO EMP VALUES
        (7521, ‘WARD’,   ‘SALESMAN’,  7698,
        TO_DATE(’22-FEB-1981′, ‘DD-MON-YYYY’), 1250,  500, 30);
INSERT INTO EMP VALUES
        (7566, ‘JONES’,  ‘MANAGER’,   7839,
        TO_DATE(‘2-APR-1981’, ‘DD-MON-YYYY’),  2975, NULL, 20);
INSERT INTO EMP VALUES
        (7654, ‘MARTIN’, ‘SALESMAN’,  7698,
        TO_DATE(’28-SEP-1981′, ‘DD-MON-YYYY’), 1250, 1400, 30);
INSERT INTO EMP VALUES
        (7698, ‘BLAKE’,  ‘MANAGER’,   7839,
        TO_DATE(‘1-MAY-1981’, ‘DD-MON-YYYY’),  2850, NULL, 30);
INSERT INTO EMP VALUES
        (7782, ‘CLARK’,  ‘MANAGER’,   7839,
        TO_DATE(‘9-JUN-1981’, ‘DD-MON-YYYY’),  2450, NULL, 10);
INSERT INTO EMP VALUES
        (7788, ‘SCOTT’,  ‘ANALYST’,   7566,
        TO_DATE(’09-DEC-1982′, ‘DD-MON-YYYY’), 3000, NULL, 20);
INSERT INTO EMP VALUES
        (7839, ‘KING’,   ‘PRESIDENT’, NULL,
        TO_DATE(’17-NOV-1981′, ‘DD-MON-YYYY’), 5000, NULL, 10);
INSERT INTO EMP VALUES
        (7844, ‘TURNER’, ‘SALESMAN’,  7698,
        TO_DATE(‘8-SEP-1981’, ‘DD-MON-YYYY’),  1500,    0, 30);
INSERT INTO EMP VALUES
        (7876, ‘ADAMS’,  ‘CLERK’,     7788,
        TO_DATE(’12-JAN-1983′, ‘DD-MON-YYYY’), 1100, NULL, 20);
INSERT INTO EMP VALUES
        (7900, ‘JAMES’,  ‘CLERK’,     7698,
        TO_DATE(‘3-DEC-1981’, ‘DD-MON-YYYY’),   950, NULL, 30);
INSERT INTO EMP VALUES
        (7902, ‘FORD’,   ‘ANALYST’,   7566,
        TO_DATE(‘3-DEC-1981’, ‘DD-MON-YYYY’),  3000, NULL, 20);
INSERT INTO EMP VALUES
        (7934, ‘MILLER’, ‘CLERK’,     7782,
        TO_DATE(’23-JAN-1982′, ‘DD-MON-YYYY’), 1300, NULL, 10);

CREATE TABLE DEPT
       (DEPTNO NUMBER(2),
        DNAME VARCHAR2(14),
        LOC VARCHAR2(13) );

INSERT INTO DEPT VALUES (10, ‘ACCOUNTING’, ‘NEW YORK’);
INSERT INTO DEPT VALUES (20, ‘RESEARCH’,   ‘DALLAS’);
INSERT INTO DEPT VALUES (30, ‘SALES’,      ‘CHICAGO’);
INSERT INTO DEPT VALUES (40, ‘OPERATIONS’, ‘BOSTON’);

CREATE TABLE BONUS
        (ENAME VARCHAR2(10),
         JOB   VARCHAR2(9),
         SAL   NUMBER,
         COMM  NUMBER);

CREATE TABLE SALGRADE
        (GRADE NUMBER,
         LOSAL NUMBER,
         HISAL NUMBER);

INSERT INTO SALGRADE VALUES (1,  700, 1200);
INSERT INTO SALGRADE VALUES (2, 1201, 1400);
INSERT INTO SALGRADE VALUES (3, 1401, 2000);
INSERT INTO SALGRADE VALUES (4, 2001, 3000);
INSERT INTO SALGRADE VALUES (5, 3001, 9999);

CREATE TABLE DUMMY
        (DUMMY NUMBER);

INSERT INTO DUMMY VALUES (0);

COMMIT;

SET TERMOUT ON
PROMPT Demonstration table build is complete.

Finding And Deleteing The Required Value From Linear Array (Unsorted)

Now many folks out there studying c++ would initially always find difficult to delete values from the unsorted array. The deleting of value from the sorted array in c++ is quit easy and quick through binary search(will be uploading it soon) but searching the value which comes multiple times in the unsorted linear array is a bit tricky for young students who have started learning programming. But here is the solution i am uploading to make the home work look easy.

Starting with the global values which should be placed before the main function.
Remember we are not using a dynamic array so we have to declare the total values of the array to be used later in the program. Muneeb is my friend name so i am using it as a array name muneeb[] you can use any name.Total_Accurance is the number of time the value to be searched comes in the array initially it is declared 0.

int Total_Value=10;
int muneeb[]={100,200,300,400,500,600,400,800,900,1000,400};
int Total_Accurance=0;

Display Array function will be used to display the values of array.

void DisplayArray()
{
        cout<<“The values in the array are :”<<endl;
        for (int q=0;q<=Total_Value;q++)
        cout<<q<<“-)  “<<” ” <<muneeb[q]<<endl;
}

int SearchItem(int Value)
{
 int index=-1;
  for (int r=0;r<=Total_Value;r++)
  {  
      if (muneeb[r]==Value)
        {
            index=r;
            Total_Accurance++;
            
        }
  }
  return index;
}

int DeleteFrom(int Loc)
{
    if(Loc>Total_Value)
        return -1;
    else
    {
        int a=muneeb[Loc];
        for(Loc;Loc<=Total_Value;Loc++)
    {
     muneeb[Loc]=muneeb[Loc+1];
    }
       Total_Value–;
        return a;
    }
}

int DeleteItemByValue(int Item)
{
    SearchItem(Item);
    const int c=Total_Accurance;
    if(c==0)
        return -1;
    else
    {
    for(int a=0;a<c;a++)
    {
         int b=SearchItem(Item);
        {
           DeleteFrom(b);
        }  
    }  
    return c;
    }

As in c++ the methods are declared twice so here they are .

void DisplayArray();
int SearchItem(int Value);
int DeleteFrom(int Loc);
int DeleteItemByValue(int Item);

This is the start of main function,

void main()
{

 cout<<“Enter the Value to be deleted from multiple locations”<<endl;
     cin>>Value;
     cout<<endl;
     int a=DeleteItemByValue(Value);
     if(a==-1)
     {
         cout<<“Value not found in the array”<<endl;
     }
     else
     {
          cout<<“Value “<<Value<<” is deleted.”<<endl;
          cout<<” It came for a total of ” <<a<<” times in the array. “<<endl;
          cout<<endl<<endl<<endl;

     cout<<“******* Finnaly the array is in the following form **************”<<endl;
     DisplayArray();
     }

Location of Max element…

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int DATA [10] = {10,12,9,7,2,3,5,25,30,23};
int K=1,LOC=1 ,N=10 , MAX=DATA[0];

while(1)
{
K=K+1;
if(K>N)
Console.Write(“Man element” + “MAX”+ “LOCATION” + “LOC” );
break;}
if(MAX<DATA[K])
{
LOC=K;
MAZ=DATA[K];
}
Console.ReadLine();
}
}
}