From 3619685f031299c73a64c7c749c093003d2fa93b Mon Sep 17 00:00:00 2001 From: Gaurav Kumar Singh <84374316+gaurav-76@users.noreply.github.com> Date: Sat, 28 Jan 2023 20:42:21 +0530 Subject: [PATCH 1/2] Implement API New Amazon OA Question Implement API --- implementAPI.java | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 implementAPI.java diff --git a/implementAPI.java b/implementAPI.java new file mode 100644 index 0000000..dffb22e --- /dev/null +++ b/implementAPI.java @@ -0,0 +1,43 @@ +public static List implementAPI(List logs) { + + Map loggedIn = new HashMap<>(); + Map password = new HashMap<>(); + + List res = new ArrayList<>(); + + for (String l : logs) { + String[] temp = l.split(" "); + if (temp[0].equals("register")) { + if (password.containsKey(temp[1])) + res.add("Username already exists"); + else { + password.put(temp[1], temp[2]); + res.add("Registered Successfully"); + } + } else if (temp[0].equals("login")) { + if (password.containsKey(temp[1]) && password.get(temp[1]).equals(temp[2])) { + if (loggedIn.containsKey(temp[1])) { + if (loggedIn.get(temp[1]) == false) { + loggedIn.put(temp[1], true); + res.add("Logged In Successfully"); + } else { + res.add("Login Unsuccessful"); + } + } else { + loggedIn.put(temp[1], true); + res.add("Logged In Successfully"); + } + } else + res.add("Login Unsuccessful"); + } else if (temp[0].equals("logout")) { + if (password.containsKey(temp[1]) && loggedIn.containsKey(temp[1]) && loggedIn.get(temp[1]) == true) { + loggedIn.put(temp[1], false); + res.add("Logged Out Successfully"); + } else { + res.add("Logout Unsuccessful"); + } + } + } + + return res; +} \ No newline at end of file From 7bdea44b475809be50934dfbdbb6fe8fa994488c Mon Sep 17 00:00:00 2001 From: Gaurav Kumar Singh <84374316+gaurav-76@users.noreply.github.com> Date: Sat, 28 Jan 2023 20:50:33 +0530 Subject: [PATCH 2/2] Implement API cpp Code New Amazon OA Question Implement API C++ Code --- implementAPI.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 implementAPI.cpp diff --git a/implementAPI.cpp b/implementAPI.cpp new file mode 100644 index 0000000..2fa56d3 --- /dev/null +++ b/implementAPI.cpp @@ -0,0 +1,47 @@ +vector implementAPI(vector &logs){ +vector ans; +map> mp; +for(string&s : logs){ + vector temp; + stringstream str(s); + string word; + while(str >> word){ + temp.push_back(word); + } + string first = temp[0]; + string user = temp[1]; + if(first == "register"){ + string pass = temp[2]; + if(mp.find(user) != mp.end()){ + ans.push_back("Username already exists"); + } + else{ + mp[user] = {pass, false}; + ans.push_back("Registered Successfully"); + } + } + else if(first == "login"){ + string pass = temp[2]; + if(mp.find(user) == mp.end()){ + ans.push_back("Login Unsuccessful"); + } + else if(mp[user].first != pass || mp[user].second == true){ + ans.push_back("Login Unsuccessful"); + } + else if(mp[user].first == pass){ + ans.push_back("Logged In Successfully"); + mp[user].second = true; + } + } + else if(first == "logout"){ + if(mp.find(user) == mp.end() || mp[user].second == false){ + ans.push_back("Logout Unsuccessful"); + } + else{ + ans.push_back("Logged Out Successfully"); + mp[user].second = false; + } + } +} +return ans; +} \ No newline at end of file