Amazon Interview Question for SDE-2s


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RuleEngine {
private static Map<String, Rules> rulesMap = new HashMap<>();

public static Map<String, Rules> parseRules() {
try {
Scanner scanner = new Scanner(
new File("/Users/biswajit/workspace/DSA/src/main/java/javacode/rules/ruleengine.csv"));
int i = 1;
while (scanner.hasNextLine()) {
Rules rules = new Rules();
String line = scanner.nextLine();
String[] words = line.split(",");
for (String word: words) {
rules.addInput(word);
}
rules.setOutput(words[words.length - 1]);
rules.getInputs().removeLast();
rulesMap.put("Rule"+i, rules);
i++;
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
return rulesMap;
}

public static String route(String rules) {

String[] rulesArr = rules.split(",");

for (Rules rule : rulesMap.values()) {
System.out.println("rulesMap.value: "+rule.toString());
int index = 0;
if (rulesArr.length == rule.getInputs().size()) {
boolean flag = true;
for (String input : rule.getInputs()) {
if (!input.contains("*") && !rulesArr[index].equals(input)) {
flag = false;
}
index++;
}
if (flag) {
return "Input maps with an existing rule engine " +
"and the output is : " + rule.getOutput();
}

}
}
return null;
}
public static void main(String[] args) {
parseRules();

String output = route("prod1, aa, ca1");
if (Objects.nonNull(output)) {
System.out.println(output);
} else {
System.out.println("Invalid Rule");
}
}
}

********************
public class Rules {
List<String> inputs = new ArrayList<>();
String output;

public List<String> getInputs() {
return inputs;
}

public void addInput(String input) {
inputs.add(input);
}

public String getOutput() {
return output;
}

public void setOutput(String output) {
this.output = output;
}
public String toString() {
return "inputs: "+this.inputs + " output: " + this.output;
}
}
*******************
csv file:
prod1, usd, us, b1
prod1, usd, *, b2
prod1, *, us, b3
prod1, *, ca, b4

- Anonymous April 02, 2024 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RuleEngine {
    private static Map<String, Rules> rulesMap = new HashMap<>();

    public static Map<String, Rules> parseRules() {
        try {
            Scanner scanner = new Scanner(
                    new File("/Users/biswajit/workspace/DSA/src/main/java/javacode/rules/ruleengine.csv"));
            int i = 1;
            while (scanner.hasNextLine()) {
                Rules rules = new Rules();
                String line = scanner.nextLine();
                String[] words = line.split(",");
                for (String word: words) {
                    rules.addInput(word);
                }
                rules.setOutput(words[words.length - 1]);
                rules.getInputs().removeLast();
                rulesMap.put("Rule"+i, rules);
                i++;
            }
            scanner.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rulesMap;
    }

    public static String route(String rules) {

        String[] rulesArr = rules.split(",");

        for (Rules rule : rulesMap.values()) {
            System.out.println("rulesMap.value: "+rule.toString());
            int index = 0;
            if (rulesArr.length == rule.getInputs().size()) {
                boolean flag = true;
                for (String input : rule.getInputs()) {
                    if (!input.contains("*") && !rulesArr[index].equals(input)) {
                        flag = false;
                    }
                    index++;
                }
                if (flag) {
                    return "Input maps with an existing rule engine " +
                            "and the output is : " + rule.getOutput();
                }

            }
        }
        return null;
    }
    public static void main(String[] args) {
        parseRules();

        String output = route("prod1, aa, ca1");
        if (Objects.nonNull(output)) {
            System.out.println(output);
        } else {
            System.out.println("Invalid Rule");
        }
    }
}

********************
public class Rules {
    List<String> inputs = new ArrayList<>();
    String output;

    public List<String> getInputs() {
        return inputs;
    }

    public void addInput(String input) {
        inputs.add(input);
    }

    public String getOutput() {
        return output;
    }

    public void setOutput(String output) {
        this.output = output;
    }
    public String toString() {
        return "inputs: "+this.inputs + " output: " + this.output;
    }
}
*******************
csv file:
prod1, usd, us, b1
prod1, usd, *, b2
prod1, *, us, b3
prod1, *, ca, b4

- Biswajit April 02, 2024 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RuleEngine {
    private static Map<String, Rules> rulesMap = new HashMap<>();

    public static Map<String, Rules> parseRules() {
        try {
            Scanner scanner = new Scanner(
                    new File("/Users/biswajit/workspace/DSA/src/main/java/javacode/rules/ruleengine.csv"));
            int i = 1;
            while (scanner.hasNextLine()) {
                Rules rules = new Rules();
                String line = scanner.nextLine();
                String[] words = line.split(",");
                for (String word: words) {
                    rules.addInput(word);
                }
                rules.setOutput(words[words.length - 1]);
                rules.getInputs().removeLast();
                rulesMap.put("Rule"+i, rules);
                i++;
            }
            scanner.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rulesMap;
    }

    public static String route(String rules) {

        String[] rulesArr = rules.split(",");

        for (Rules rule : rulesMap.values()) {
            System.out.println("rulesMap.value: "+rule.toString());
            int index = 0;
            if (rulesArr.length == rule.getInputs().size()) {
                boolean flag = true;
                for (String input : rule.getInputs()) {
                    if (!input.contains("*") && !rulesArr[index].equals(input)) {
                        flag = false;
                    }
                    index++;
                }
                if (flag) {
                    return "Input maps with an existing rule engine " +
                            "and the output is : " + rule.getOutput();
                }

            }
        }
        return null;
    }
    public static void main(String[] args) {
        parseRules();

        String output = route("prod1, aa, ca1");
        if (Objects.nonNull(output)) {
            System.out.println(output);
        } else {
            System.out.println("Invalid Rule");
        }
    }
}

********************
public class Rules {
    List<String> inputs = new ArrayList<>();
    String output;

    public List<String> getInputs() {
        return inputs;
    }

    public void addInput(String input) {
        inputs.add(input);
    }

    public String getOutput() {
        return output;
    }

    public void setOutput(String output) {
        this.output = output;
    }
    public String toString() {
        return "inputs: "+this.inputs + " output: " + this.output;
    }
}
*******************
csv file:
prod1, usd, us, b1
prod1, usd, *, b2
prod1, *, us, b3
prod1, *, ca, b4

- biswajit190101 April 02, 2024 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More