Re: an old worn interview question
- From: Tegiri Nenashi <TegiriNenashi@xxxxxxxxx>
- Date: Mon, 17 Sep 2007 16:21:47 -0700
On Sep 17, 3:00 pm, user923005 <dcor...@xxxxxxxxx> wrote:
A typical problem is a database of workers and supervisors. All
employees are kept in a single table, where employees of supervisors
are identified with a 'works_for' field. I guess you can imagine how
easily loops can arise.
A proper algorithm will identify the loops and draw an org chart, with
defects spelled out in some way. You can even do the identification
phase purely in SQL if you use a temp table.
If your organizational chart is a tree, then why do you represent it
as a DAG? You can leverage nested sets, therefore automatically
enforcing tree constraint.
OK, even if you represent the tree as a graph, then you can just query
the number of connected components in the graph. A connected graph as
a graph that has a single connected component. Next, a connected graph
with N nodes and N-1 edges must be a tree. Thus, counting nodes and
edges together with transitive closure is a way to enforce tree
constraint.
And, finally, one of the "advanced" techniques that I recall from my
interview many years ago was initiating two pointer with different
speeds and watching when one pointer reaches the other. One may wonder
how this solution might be helpful in SQL world.
.
- References:
- an old worn interview question
- From: Tegiri Nenashi
- Re: an old worn interview question
- From: user923005
- an old worn interview question
- Prev by Date: Re: source code of built-in functions
- Next by Date: Re: an old worn interview question
- Previous by thread: Re: an old worn interview question
- Next by thread: Re: an old worn interview question
- Index(es):
Relevant Pages
|