From 1a38c53b2ce3ee2fdd395916a068204c1d05ebfe Mon Sep 17 00:00:00 2001 From: Shanthan <> Date: Fri, 21 Apr 2023 10:42:37 -0700 Subject: [PATCH 1/3] Used the Queue Data Structure for the hire method since the Org is designed like a tree with multiple levels --- dotnet/MyOrganization/Employee.cs | 4 ++-- dotnet/MyOrganization/Organization.cs | 25 +++++++++++++++++++++++++ dotnet/MyOrganization/Position.cs | 2 +- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/dotnet/MyOrganization/Employee.cs b/dotnet/MyOrganization/Employee.cs index 208bd8e..1323123 100644 --- a/dotnet/MyOrganization/Employee.cs +++ b/dotnet/MyOrganization/Employee.cs @@ -12,7 +12,7 @@ internal class Employee private int identifier; private Name name; - public Employee(int identifier, Name name) + public Employee(Name name) { if (name == null) throw new Exception("name cannot be null"); @@ -32,7 +32,7 @@ public Name GetName() override public string ToString() { - return name.ToString() + ": " + identifier; + return name.ToString(); } } } diff --git a/dotnet/MyOrganization/Organization.cs b/dotnet/MyOrganization/Organization.cs index e288ef8..f88687d 100644 --- a/dotnet/MyOrganization/Organization.cs +++ b/dotnet/MyOrganization/Organization.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.ConstrainedExecution; using System.Text; using System.Threading.Tasks; @@ -27,6 +28,30 @@ public Organization() public Position? Hire(Name person, string title) { //your code here + + Position current = root; + + Queue queue = new Queue(); + + queue.Enqueue(current); + + while (queue.Count > 0) + { + Position cur = queue.Dequeue(); + + if (cur.title == title) + { + cur.SetEmployee(new Employee(person)); + return cur; + } + else + { + foreach (Position pos in cur.GetDirectReports()) + { + queue.Enqueue(pos); + } + } + } return null; } diff --git a/dotnet/MyOrganization/Position.cs b/dotnet/MyOrganization/Position.cs index d02eb30..d4177f6 100644 --- a/dotnet/MyOrganization/Position.cs +++ b/dotnet/MyOrganization/Position.cs @@ -10,7 +10,7 @@ namespace MyOrganization { internal class Position { - private string title; + public string title; private Employee? employee; private HashSet directReports; From 812ecabf16691cb2536cd64a01245624a0e6e958 Mon Sep 17 00:00:00 2001 From: Shanthan <> Date: Fri, 21 Apr 2023 10:49:45 -0700 Subject: [PATCH 2/3] Another UseCase when no position has title and Comments Added for my Approach --- dotnet/MyOrganization/Organization.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/dotnet/MyOrganization/Organization.cs b/dotnet/MyOrganization/Organization.cs index f88687d..9238d61 100644 --- a/dotnet/MyOrganization/Organization.cs +++ b/dotnet/MyOrganization/Organization.cs @@ -25,6 +25,12 @@ public Organization() * @param title * @return the newly filled position or empty if no position has that title */ + + + /* + APPROACH EXPLANATION : The CreateOrganization is built like a Tree with multiple Levels . + One of the Recommended Approaches that is done iteratively is using a Queue by exploring each position and its Direct Reported Positions.. + */ public Position? Hire(Name person, string title) { //your code here @@ -38,7 +44,7 @@ public Organization() while (queue.Count > 0) { Position cur = queue.Dequeue(); - + // validate if the position has the that title if (cur.title == title) { cur.SetEmployee(new Employee(person)); @@ -52,7 +58,9 @@ public Organization() } } } - return null; + // return empty is no position has title + // different use case.. + return new Position(string.Empty); } override public string ToString() From 89d1139f37ee85b03d789e75b584e49300d90a9f Mon Sep 17 00:00:00 2001 From: Shanthan <> Date: Sun, 18 Jun 2023 10:08:42 -0700 Subject: [PATCH 3/3] Added the identifier --- dotnet/MyOrganization/Employee.cs | 4 ++-- dotnet/MyOrganization/Organization.cs | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/dotnet/MyOrganization/Employee.cs b/dotnet/MyOrganization/Employee.cs index 1323123..cf45f2f 100644 --- a/dotnet/MyOrganization/Employee.cs +++ b/dotnet/MyOrganization/Employee.cs @@ -12,11 +12,11 @@ internal class Employee private int identifier; private Name name; - public Employee(Name name) + public Employee(Name name, int Identifier) { if (name == null) throw new Exception("name cannot be null"); - this.identifier = identifier; + this.identifier = Identifier; this.name = name; } diff --git a/dotnet/MyOrganization/Organization.cs b/dotnet/MyOrganization/Organization.cs index 9238d61..69f145f 100644 --- a/dotnet/MyOrganization/Organization.cs +++ b/dotnet/MyOrganization/Organization.cs @@ -10,7 +10,7 @@ namespace MyOrganization internal abstract class Organization { private Position root; - + private int iden; public Organization() { root = CreateOrganization(); @@ -25,8 +25,6 @@ public Organization() * @param title * @return the newly filled position or empty if no position has that title */ - - /* APPROACH EXPLANATION : The CreateOrganization is built like a Tree with multiple Levels . One of the Recommended Approaches that is done iteratively is using a Queue by exploring each position and its Direct Reported Positions.. @@ -34,9 +32,8 @@ public Organization() public Position? Hire(Name person, string title) { //your code here - Position current = root; - + iden = 0; Queue queue = new Queue(); queue.Enqueue(current); @@ -47,11 +44,16 @@ public Organization() // validate if the position has the that title if (cur.title == title) { - cur.SetEmployee(new Employee(person)); - return cur; + // if not filled... + if(!cur.IsFilled()) + { + cur.SetEmployee(new Employee(person,iden++)); + return cur; + } } else { + //enqueuing the current position Direct Reports into the Queue for exploration... foreach (Position pos in cur.GetDirectReports()) { queue.Enqueue(pos);