Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle author's reputation when same user's vote is removed in favour of new vote. #60

Open
umangahuja1 opened this issue Dec 21, 2024 · 0 comments

Comments

@umangahuja1
Copy link

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant