MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SQL & Linked List Challenge

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
Siva Kumar Sunku
Beginner


Joined: 17 Aug 2004
Posts: 25
Topics: 14

PostPosted: Thu Mar 24, 2005 4:03 am    Post subject: SQL & Linked List Challenge Reply with quote


#1. I have the following table, I want the result to be perculated down the hierarchy.

Table Contents
Code:

EMP_ID   EMP_NAME   SUPERIOR_ID
------   --------   -----------
0001   PQR      NULL
0002   XYZ      0001
0003   ABC      0001
0004   MNC      0001
0005   IEK      0002
1234   ABCD      NULL
1001   EFGH      1234
1002   IJKL      1001
0006   SQL      0005

If I provide Input string '0001', Result should look like
Code:

EMP_ID      SUPERIOR_NAME   EMP_NAME
--------   -------------   --------
0001      (Blank)      PQR
0002      PQR      XYZ
0003      PQR      ABC
0004      PQR      MNC
0005      XYZ      IEK
0006      IEK      SQL

If I provide Input string '0002', Result should look like
Code:

EMP_ID      SUPERIOR_NAME   EMP_NAME
--------   -------------   --------
0002      PQR      XYZ
0005      XYZ      IEK
0006      IEK      SQL


#2. Algorithm to find the whether Linked list is Circular or not in Order of N.
Note:- Linked List may be partial circular.

Moved to the database Forum by Moderator
Back to top
View user's profile Send private message AIM Address
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Thu Mar 24, 2005 5:13 am    Post subject: Reply with quote

Siva Kumar Sunku,

Quote:

If I provide Input string '0001', Result should look like


Input string to ??? Emp_id, or Superior_id

And where is the Superior_Name coming from?

You need to explain your challenge .

Quote:

2. Algorithm to find the whether Linked list is Circular or not in Order of N.
Note:- Linked List may be partial circular.


Which language? Do you want to do it via SQL? or any other programming language like cobol , C++...?

btw I guess you know the rules of this forum. I assume that this is a CHALLENGE question and not a question seeking help.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Umesha N Shetty
Beginner


Joined: 16 Apr 2005
Posts: 2
Topics: 0

PostPosted: Sat Apr 16, 2005 9:23 am    Post subject: Reply with quote

kolusu,

Looks like Siva Kumar Sunku did not reply to your queries.

This challenge is one of my favourites for years. So, let me try to answer the queries.

In one sentence, challenge is :
Find all employees in a given employee's own org structure

i.e. Self
+ Direct Reports
+ Indirect reports (all direct reports of your direct reports or indirect reports)


Quote:
Input string to ??? Emp_id, or Superior_id


Both. Initially it will be used against Emp_id and then against Superior _id

Quote:
And where is the Superior_Name coming from?


Every Superior is an Employee. So Superior_Name comes from EMP_NAME
This is a SELF-LOOP table. It is a parent/child of itself.

Siva Kumar Sunku,

(i) ORACLE has a SQL Clause named START to handle self loops easily. I dont think ANSI SQL has any such feature. If Oracle specific responses are acceptable, I can post it here.

(ii) Without a feature like the above, solution will have to use a CURSOR based logic with loops. Solution would go something like below:

Code:

Ids_in_the_list = 1
Last_id_index = 0

While  (Last_id < Ids_in_the_list )

       select a.emp_id
       from emp a
       where a.superior_id = Id_list(Last_id_index)

'      Add these outputs to the Id_list and increment
       Ids_in_the_list by number of rows added.

       increment Last_id_index by 1

While-end

For  i = 1 to  Id_in_the_list
       select a.emp_id, b.emp_name as superior_name,  b.emp_name
       from emp a, emp b
       where a. emp_id = Id_list(i)  AND b.emp_id = a.superior_id
'      This is the result

Next i



Better solutions may be possible, if temporary tables are used for Id_List. I am not sure whether following would work But if it does , it would be great.

Code:


'  insert temp1 table emp_id value(Input_id)

While  (rowcount >0 )

       select a.emp_id
       from emp a
       where a.superior_id in (select * from temp1)
       into temp1

       Ids_in_the_list = Ids_in_the_list + rowcount

While-end

Back to top
View user's profile Send private message
Umesha N Shetty
Beginner


Joined: 16 Apr 2005
Posts: 2
Topics: 0

PostPosted: Sat Apr 16, 2005 9:36 am    Post subject: Reply with quote

Siva Kumar Sunku,

About the algorithm question:

Quote:

#2. Algorithm to find the whether Linked list is Circular or not in Order of N.
Note:- Linked List may be partial circular.


Whether the linked ls circular or not can be checked by checking Id_List contents before inserting a new one to it. Same applies to temp1 in the temp table version.

I did not understand the following:
Quote:

..... not in Order of N. Note:- Linked List may be partial circular.


What is the Order of N here ? I never heard/seen anybody using Order term in Linked List context. Are you talking about the depth of the org structure here in this example ? If so, it can be easily detected - as it is number of while loop execution. START in Oracle also has a feature to get that info.

Also, I did not understand , why/how the list can be partial circular ? In the employee table example, I could not see such possibility.

Anyway, circualr linked list can be detected as mentioned above, and can be skipped. So if they are not added to the Id_list (or temp1) secodn time, it will not cause any problems.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group