Simonefleek / Studies-

0 stars 0 forks source link

Blackrock pre-screening interview #3

Open Simonefleek opened 3 weeks ago

Simonefleek commented 3 weeks ago

rogramming challenge description: A company wants to track change in their organization by knowing how many levels exist between any two employees. This number will help them know who is being promoted and who is not.

For example: If Susan reports to John and John reports to Amy. Then, there are 2 level between Susan and Amy.

Write a program that will count how many levels exist between any two names in a hierarchy of employees. The program must read a list of name pairs that represent an employee and their manager.

HINT: The two names to compare may be in different parts of the organizational tree and not have a direct managerial line.

Input: The first line of input will be a pair of names to compare.

All subsequent lines will be employee/manager pairs. The company's complete hierarchy will be included so no incomplete trees will exist.

For example:

Susan/Amy Susan/John John/Amy Output: The number of levels between the pair requested, including the top manager's level. In the example above, the Output should be: 2

Test 1

Test Input Download Test 1 Input Susan/Amy Susan/John John/Amy Expected Output Download Test 1 Output 2 Test 2

Test Input Download Test 2 Input Scott/David Terry/David Kyle/David Ben/Kyle Scott/Jon Chris/Scott Jon/Kenny Kenny/David David/Mike Expected Output Download Test 2 Output 3 Test 3

Test Input Download Test 3 Input Ben/Jon Terry/David Kyle/David Ben/Kyle Scott/Jon Chris/Scott Jon/Kenny Kenny/David Expected Output Download Test 3 Output 0 Test 4

Test Input Download Test 4 Input Christy/Susan Aimee/Melissa Melissa/Susan Stacy/Corinne Gabrielle/Melissa Corinne/Melissa Christy/Stacy Pat/Christy Expected Output Download Test 4 Output 4

Simonefleek commented 3 weeks ago

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.*;

public class Main { public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8); BufferedReader in = new BufferedReader(reader); String line; List input = new ArrayList<>();

    while ((line = in.readLine()) != null && !line.isEmpty()) {
        input.add(line);
    }

    // Parse input
    String[] comparePair = input.get(0).split("/");
    List<String[]> hierarchy = new ArrayList<>();
    for (int i = 1; i < input.size(); i++) {
        hierarchy.add(input.get(i).split("/"));
    }

    // Find levels between the two employees
    int levels = findLevelsBetweenEmployees(comparePair[0], comparePair[1], hierarchy);
    System.out.println(levels);
}

public static int findLevelsBetweenEmployees(String employee1, String employee2, List<String[]> hierarchy) {
    // Create a graph from the hierarchy
    Map<String, List<String>> graph = new HashMap<>();
    for (String[] pair : hierarchy) {
        graph.putIfAbsent(pair[0], new ArrayList<>());
        graph.putIfAbsent(pair[1], new ArrayList<>());
        graph.get(pair[0]).add(pair[1]);
        graph.get(pair[1]).add(pair[0]);
    }

    // BFS to find the shortest path between employee1 and employee2
    Queue<String> queue = new LinkedList<>();
    Map<String, Integer> levels = new HashMap<>();
    queue.add(employee1);
    levels.put(employee1, 0);

    while (!queue.isEmpty()) {
        String currentEmployee = queue.poll();
        int currentLevel = levels.get(currentEmployee);

        if (currentEmployee.equals(employee2)) {
            return currentLevel;
        }

        for (String neighbor : graph.get(currentEmployee)) {
            if (!levels.containsKey(neighbor)) {
                queue.add(neighbor);
                levels.put(neighbor, currentLevel + 1);
            }
        }
    }

    return 0; // If no path is found, return 0
}

}

Simonefleek commented 3 weeks ago

Their is an error with output 3 Running test cases... Done – – – – – – – – – – – – – – Test 1

Passed Expand Test 2

Passed Expand Test 3 Failed Test Input: Ben/Jon Terry/David Kyle/David Ben/Kyle Scott/Jon Chris/Scott Jon/Kenny Kenny/David Expected Output: 0 Your Output: 4 Test 4

Passed Expand