Atlassian P50 Interview Experience
I recently appeared for the Atlassian Karat Interview.
I was asked one leetcode medium questions and some scenario based design question.
After 2 days, I got call from recruiter saying that I cleared it.
And they scheduled Code design round after 2days.
I prepared DSA for it, and I was asked below question
We need to maintain the average rating of the user in the system.
We will recieve many request which will contain the rating of the user.
Implement/design a solution which can calculate the average rating of the user and at last we need to print the rating of all the user in the system with descending order of their rating.
I was trying to discuss the solution with the interviewer but he was not attentive during the call. I Implemented the below solution but am not sure will I able to make it or not.
Let me know in comments if you can think of any better solution
public class RatingServiceClass {
PriorityQueue<User> userList=new PriorityQueue<>((a,b)-> a.rating==b.rating?
a.name.compareTo(b.name): b.rating-a.rating);
Map<Integer,User> userMap = new HashMap<>();
public void addRatingForTheUser(User user,int rating){
if(userMap.containsKey(user.id)){
int totalSum = user.ratingSum+rating;
int totalCount=user.countRating+1;
int avgRating = totalSum/totalCount;
user.setRating(avgRating);
user.setRatingSum(totalSum);
user.setCountRating(totalCount);
}else{
user.setRating(rating);
user.setRatingSum(rating);
user.setCountRating(1);
userList.add(user);
}
userMap.put(user.id,user);
}
public void getRatingsForEveryUser(){
for(User user:userList){
System.out.println("User is : "+user.getName()+" Rating is : "+user.getRating());
}
}}
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
RatingServiceClass ratingServiceClass = new RatingServiceClass();
User user = new User(1,"UserA",4);
User user1 = new User(2,"UserB",3);
ratingServiceClass.addRatingForTheUser(user,4);
ratingServiceClass.addRatingForTheUser(user1,3);
ratingServiceClass.getRatingsForEveryUser();
}}
public class User {
int id;
String name;
int rating;
int countRating;
int ratingSum;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
public int getCountRating() {
return countRating;
}
public void setCountRating(int countRating) {
this.countRating = countRating;
}
public int getRatingSum() {
return ratingSum;
}
public void setRatingSum(int ratingSum) {
this.ratingSum = ratingSum;
}
public User(int id,String name, int rating) {
this.id=id;
this.name = name;
this.rating = rating;
this.countRating=1;
}}
#Atlassian


