Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion java/com/aa/act/interview/org/Organization.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.aa.act.interview.org;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Queue;

public abstract class Organization {

private Position root;
private int employeeIdentifier;

public Organization() {
employeeIdentifier = 0;
root = createOrganization();
}

Expand All @@ -21,7 +26,10 @@ public Organization() {
*/
public Optional<Position> hire(Name person, String title) {
//your code here
return Optional.empty();
Position hiredPosition = bfsGetPositionPerTitle(title);
hiredPosition.setEmployee(Optional.of(new Employee(++employeeIdentifier, person)));
return Optional.ofNullable(hiredPosition);
//return Optional.empty();
}

@Override
Expand All @@ -36,4 +44,26 @@ private String printOrganization(Position pos, String prefix) {
}
return sb.toString();
}

public Position bfsGetPositionPerTitle(String title) {
HashSet<Position> visited = new HashSet<>();
Queue<Position> adjacent = new LinkedList<>();
adjacent.add(root);

// Loop through all adjacent positions
while (!adjacent.isEmpty()) {
Position current = adjacent.remove();
if (current.getTitle() == title) {
return current;
}
for (Position position : current.getDirectReports()) {
if (!visited.contains(position)) {
adjacent.add(position);
}
}
visited.add(current);
}
throw new IllegalArgumentException("there is no position with this title");

}
}