You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a user has earlier upvoted/downvoted, shouldn't author's reputation update accordingly?
Example
Starting with 0 votes and 0 author reputation
User 1 upvotes -> [(User 1, +1)] and reputation = +10
Now User 1 upvotes again, first we should prevent it altogether.
But if we are saying removeIf will take care of it, then we should update reputation while removing as well.
So that subsequent upvotes and downvotes are nullified.
Continuing on above example, say if User 1 has upvotes again,
we would want to retain the reputation 10 and votes identical (since we will remove it and add again)
So while removing, reputation should also be modified.
public void vote(User user, int value) {
if (value != 1 && value != -1) {
throw new IllegalArgumentException("Vote value must be either 1 or -1");
}
votes.removeIf(v -> v.getUser().equals(user));
votes.add(new Vote(user, value));
author.updateReputation(value * 10); // +10 for upvote, -10 for downvote
}
Also considering only check by user, we could have used HashMap instead of List.
And to improve fetching the total vote count, we could have kept a local variable and modify that per vote operation.
The text was updated successfully, but these errors were encountered:
https://github.com/ashishps1/awesome-low-level-design/blob/774631a82c9c0848352197e55c649fda187325c6/solutions/java/src/stackoverflow/Answer.java#L28C1-L36C6
If a user has earlier upvoted/downvoted, shouldn't author's reputation update accordingly?
Example
Starting with 0 votes and 0 author reputation
User 1 upvotes -> [(User 1, +1)] and reputation = +10
Now User 1 upvotes again, first we should prevent it altogether.
But if we are saying removeIf will take care of it, then we should update reputation while removing as well.
So that subsequent upvotes and downvotes are nullified.
Continuing on above example, say if User 1 has upvotes again,
we would want to retain the reputation 10 and votes identical (since we will remove it and add again)
So while removing, reputation should also be modified.
Also considering only check by user, we could have used HashMap instead of List.
And to improve fetching the total vote count, we could have kept a local variable and modify that per vote operation.
The text was updated successfully, but these errors were encountered: