PayPal Interview Question
Quality Assurance EngineersCountry: United States
Interview Type: Phone Interview
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class T1 {
public static void main(String[] args) {
Map <String, String> book = new HashMap();
Map <String, List<String>> result = new HashMap();
book.put("Java", "John");
book.put("C#", "Rob");
book.put("Ruby", "John");
book.put("Rails", "Rob");
result.put("John", new ArrayList<String>());
result.put("Rob", new ArrayList<String>());
for (Map.Entry<String, String> entry : book.entrySet()) {
if(entry.getValue().equals("John")) {
result.get("John").add(entry.getKey());
}
if(entry.getValue().equals("Rob")) {
result.get("Rob").add(entry.getKey());
}
}
System.out.println(" Map Elements");
System.out.print("\t" + result);
}
}
import java.util.*;
public class PaypalAssignment {
public static void main(String[] args)
{
// TODO Auto-generated method stub
Map<String,String> bookMap=new HashMap<String,String>();
Map<String,ArrayList> newArrayMap=new HashMap<String,ArrayList>();
HashSet set=new HashSet();
ArrayList list=new ArrayList();
ArrayList list2=new ArrayList();
String key;
bookMap.put("C#", "John");
bookMap.put("Java", "Rob");
bookMap.put("Ruby", "John");
bookMap.put("JScript", "Rob");
for(Map.Entry<String,String> entry:bookMap.entrySet())
{
if(!set.contains(entry.getValue()))
{
set.add(entry.getValue());
}
switch(entry.getValue())
{
case "John":
list.add(entry.getKey());
newArrayMap.put(entry.getValue(), list);
break;
case "Rob":
list2.add(entry.getKey());
newArrayMap.put(entry.getValue(), list2);
break;
default:
break;
}
}
Set set1=newArrayMap.entrySet();
Iterator i=set1.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
import java.util.*;
public class PaypalAssignment {
public static void main(String[] args)
{
// TODO Auto-generated method stub
Map<String,String> bookMap=new HashMap<String,String>();
Map<String,ArrayList> newArrayMap=new HashMap<String,ArrayList>();
HashSet set=new HashSet();
ArrayList list=new ArrayList();
ArrayList list2=new ArrayList();
String key;
bookMap.put("C#", "John");
bookMap.put("Java", "Rob");
bookMap.put("Ruby", "John");
bookMap.put("JScript", "Rob");
for(Map.Entry<String,String> entry:bookMap.entrySet())
{
if(!set.contains(entry.getValue()))
{
set.add(entry.getValue());
}
switch(entry.getValue())
{
case "John":
list.add(entry.getKey());
newArrayMap.put(entry.getValue(), list);
break;
case "Rob":
list2.add(entry.getKey());
newArrayMap.put(entry.getValue(), list2);
break;
default:
break;
}
}
Set set1=newArrayMap.entrySet();
Iterator i=set1.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
package dataStrucures.practicals;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class PaypalAssignment {
public static void main(String a[]){
Map<String,String> map=new ConcurrentHashMap<>();
map.put("Java","John");
map.put("C#","Rob");
map.put("Ruby","John");
map.put("Rails","Rob");
map.put("Junit","John");
map.put("python","Rob");
map.put("C++","John");
map.put(".Net","Rob");
mapping(map).forEach((key,Value)-> System.out.println(key+":"+Value));
}
public static Map<String, List> mapping(Map<String, String> booksMap){
Map<String, List> finalMap= new HashMap();
Set<String> keys = booksMap.keySet();
Iterator<String> it=keys.iterator();
while(it.hasNext()){
String key_value = it.next();
String temp_value = booksMap.get(key_value);
List<String> values = new ArrayList<String>();
for (Map.Entry<String, String> entry : booksMap.entrySet()) {
if(entry.getValue().equals(temp_value)) {
values.add(entry.getKey());
}
}
finalMap.put(temp_value, values);
}
return finalMap;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
public class Hashmap {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
HashMap m = new HashMap<String, String>();
HashMap<String, ArrayList> resultMap = new HashMap<String, ArrayList>();
m.put("Ruby", "Jhon");
m.put("Java", "Jhon");
m.put("C#", "Rob");
m.put("Python", "Rob");
m.forEach((key, value) -> {
if (!resultMap.containsKey(value)) {
ArrayList l = new ArrayList<>();
l.add(key);
resultMap.put((String) value, l);
} else {
resultMap.get(value).add(key);
}
});
resultMap.forEach((i,k)->{System.out.println(i+","+k);});
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
public class Hashmap {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
HashMap m = new HashMap<String, String>();
HashMap<String, ArrayList> resultMap = new HashMap<String, ArrayList>();
m.put("Ruby", "Jhon");
m.put("Java", "Jhon");
m.put("C#", "Rob");
m.put("Python", "Rob");
m.forEach((key, value) -> {
if (!resultMap.containsKey(value)) {
ArrayList l = new ArrayList<>();
l.add(key);
resultMap.put((String) value, l);
} else {
resultMap.get(value).add(key);
}
});
resultMap.forEach((i,k)->{System.out.println(i+","+k);});
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
public class Hashmap {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
HashMap m = new HashMap<String, String>();
HashMap<String, ArrayList> resultMap = new HashMap<String, ArrayList>();
m.put("Ruby", "Jhon");
m.put("Java", "Jhon");
m.put("C#", "Rob");
m.put("Python", "Rob");
m.forEach((key, value) -> {
if (!resultMap.containsKey(value)) {
ArrayList l = new ArrayList<>();
l.add(key);
resultMap.put((String) value, l);
} else {
resultMap.get(value).add(key);
}
});
resultMap.forEach((i,k)->{System.out.println(i+","+k);});
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
public class Hashmap {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
HashMap m = new HashMap<String, String>();
HashMap<String, ArrayList> resultMap = new HashMap<String, ArrayList>();
m.put("Ruby", "Jhon");
m.put("Java", "Jhon");
m.put("C#", "Rob");
m.put("Python", "Rob");
m.forEach((key, value) -> {
if (!resultMap.containsKey(value)) {
ArrayList l = new ArrayList<>();
l.add(key);
resultMap.put((String) value, l);
} else {
resultMap.get(value).add(key);
}
});
resultMap.forEach((i,k)->{System.out.println(i+","+k);});
}
}
mybook={}
print("Enter the no of keys u want to enter")
n=int(input())
for i in range(0,n):
key=input("Enter the key")
value=input("Enter the value")
mybook[key]=value
print(mybook)
authors={}
key_list=[]
used=[]
key_list=mybook.keys()
for i in key_list:
v=mybook.get(i)
if v not in used:
used.append(v)
authors.setdefault(v,[])
authors[v].append(i)
print(authors)
mybook={}
print("Enter the no of keys u want to enter")
n=int(input())
for i in range(0,n):
key=input("Enter the key")
value=input("Enter the value")
mybook[key]=value
print(mybook)
authors={}
key_list=[]
used=[]
key_list=mybook.keys()
for i in key_list:
v=mybook.get(i)
if v not in used:
used.append(v)
authors.setdefault(v,[])
authors[v].append(i)
print(authors)
mybook={}
print("Enter the no of keys u want to enter")
n=int(input())
for i in range(0,n):
key=input("Enter the key")
value=input("Enter the value")
mybook[key]=value
print(mybook)
authors={}
key_list=[]
used=[]
key_list=mybook.keys()
for i in key_list:
v=mybook.get(i)
if v not in used:
used.append(v)
authors.setdefault(v,[])
authors[v].append(i)
print(authors)
var data2 = [{'Java' : 'John'},{'C#':'Rob'},{'Ruby':'John'},{'Rails':'Rob'},{'Fuss':'Ruchi'},{'stud':'Sud'}];
function getUniqueBook(){
var newArr = data2.reduce((accum, item) => {
for(book in item){
var temp = item[book];
if(accum[temp])
accum[temp] = [...accum[temp],book]
else
accum[temp] =[book]
}
return accum;
},{});
console.log(newArr);
}
Map<String, String> map = new HashMap<>();
map.put("Java", "John");
map.put("C#", "Rob");
map.put("Ruby", "John");
map.put("Rails", "Rob");
map.put("Python", "John");
map.put("Go", "Rob");
Map<String, Set<String>> mapSet = new HashMap<>();
Set<String> languages = null;
for (String str : map.keySet()) {
String author = map.get(str);
Set<String> set=mapSet.getOrDefault(author,new HashSet<String>());
set.add(str);
mapSet.put(author,set);
}
mapSet.forEach((key,value)->{
System.out.println(key+" "+value);
});
}
Map<String, String> map = new HashMap<>();
map.put("Java", "John");
map.put("C#", "Rob");
map.put("Ruby", "John");
map.put("Rails", "Rob");
map.put("Python", "John");
map.put("Go", "Rob");
Map<String, Set<String>> mapSet = new HashMap<>();
Set<String> languages = null;
for (String str : map.keySet()) {
String author = map.get(str);
Set<String> set=mapSet.getOrDefault(author,new HashSet<String>());
set.add(str);
mapSet.put(author,set);
}
mapSet.forEach((key,value)->{
System.out.println(key+" "+value);
});
}
Map<String, String> map = new HashMap<>();
map.put("Java", "John");
map.put("C#", "Rob");
map.put("Ruby", "John");
map.put("Rails", "Rob");
map.put("Python", "John");
map.put("Go", "Rob");
Map<String, Set<String>> mapSet = new HashMap<>();
Set<String> languages = null;
for (String str : map.keySet()) {
String author = map.get(str);
Set<String> set=mapSet.getOrDefault(author,new HashSet<String>());
set.add(str);
mapSet.put(author,set);
}
mapSet.forEach((key,value)->{
System.out.println(key+" "+value);
});
}
- TurboMap June 26, 2018