India’s Best Job Seekers and Training Platform Forums Java Problems set Develop a service to help a client quickly…

  • Develop a service to help a client quickly…

     Akash  updated 11 months, 3 weeks ago 2 Members · 2 Posts
  • Akash 


    Develop a service to help a client quickly find a manager who can resolve the conflict between two employees. When there is a conflict between two employees, the closest common manager should help resolve the conflict. The developers plan to test the service by providing an example reporting hierarchy to enable the identification of the closest common manager for two employees. Your goal is to develop an algorithm for IBM to efficiently perform this task. To keep things simple, they just use a single relationship “isManagerOf” between any two employees. For example, consider a reporting structure represented as a set of triples:

    Tom isManagerOf Mary

    Mary isManagerOf Bob

    Mary isManagerOf Sam

    Bob isManagerOf John

    Sam isManagerOf Pete

    Sam isManagerOf Katie

    The manager who should resolve the conflict between Bob and Mary is Tom(Mary’s manager). The manager who should resolve the conflict between Pete and Katie is Sam(both employees’ manager). The manager who should resolve the conflict between Bob and Pete is Mary(Bob’s manager and Pete’s manager’s manager).


    There will be at least one isManagerOf relationship.

    There can be a maximum of 15 team member to a single manager

    No cross management would exist i.e., a person can have only one manager

    There can be a maximum of 100 levels of manager relationships in the corporation


    R1,R2,R3,R4…Rn,Person1,Person2 R1…Rn – A comma separated list of “isManagerOf” relationships. Each relationship being represented by an arrow “Manager->Person”. Person1,Person2 – The name of the two employee that have conflict


    The name of the manager who can resolve the conflict Note: Please be prepared to provide a video follow-up response to describe your approach to this exercise.

    Test 1:

    Test Input


    Expected Output


    Test 2:

    Test Input


    Expected Output


  • Akash 


    package myProject;

    import java.util.ArrayList;

    import java.util.HashMap;

    import java.util.StringTokenizer;

    public class Manager {

    public static void main(String[] args) {

    HashMap<String,String> map = new HashMap<String,String>();

    String inputString = “Frank->Mary,Mary->Sam,Mary->Bob,Sam->Katie,Sam->Pete,Bob->John,Sam,Katie”;

    String mgr = createEmployeeManagerDictionary(map,inputString);

    System.out.println(“Mgr who will resolve the issue is: ” + mgr);


    private static String createEmployeeManagerDictionary(HashMap<String, String> map, String inputString) {

    StringTokenizer tokens = new StringTokenizer(inputString,”,”);

    StringBuffer sb = new StringBuffer();

    String emp1 = null;

    String emp2 = null;

    while (tokens.hasMoreElements()) {

    String token = tokens.nextToken();


    map.put(token.split(“->”)[1], token.split(“->”)[0]);


    sb.append(token + “,”);



    tokens = new StringTokenizer(sb.toString(),”,”);

    while (tokens.hasMoreElements()) {

    emp1 = tokens.nextToken();

    emp2 = tokens.nextToken();


    String mgr = resolveIssue(emp1,emp2,map);

    return mgr;


    private static String resolveIssue(String emp1, String emp2, HashMap<String, String> map) {

    ArrayList<String> sb1 = getTree(emp1,map);

    ArrayList<String> sb2 = getTree(emp2,map);

    if(sb1.size()==0 && sb2.size()!=0){

    int i = sb2.size();

    return sb2.get(i-1);


    if(sb2.size()==0 && sb1.size()!=0){

    int i = sb1.size();

    return sb1.get(i-1);



    return sb2.get(0);


    return sb1.get(0);



    private static ArrayList<String> getTree(String emp, HashMap<String, String> map) {

    ArrayList<String> stringArray = new ArrayList<String>();



    emp = map.get(emp);


    return stringArray;



Log in to reply.

Original Post
0 of 0 posts June 2018
KodNest Training New Batch is starting on 02nd November 2020. Attend one week free demo classes.Register Now

New Report