The above code can result in unexpected result when handling simultaneous requests.
For example
Multuple instances could be duplicated for an individual customer
Multiple threads could obtain the same instance and modify its score at the same time, which could ends in unpredicatable result.
Besides this one, the other two retrieval methods are not safe neither. The list is being accessed without synchronization.
Performance
The retrieval operations (GetCustomersByRank / GetCustomersByCustomerId) sort the entrie list before each query, which is very low efficient. The proper approach is to keep the customers in order, rather than sorting it everytime before each query.
Thanks for joining our test.
https://github.com/XVCoder/CustomerScoreRank/blob/b0485b046e34596ab192066ab5eee8b500c8dcdf/CustomerScoreRank/Services/CustomerService.cs
Thread Safety
The above code can result in unexpected result when handling simultaneous requests. For example
Besides this one, the other two retrieval methods are not safe neither. The list is being accessed without synchronization.
Performance
The retrieval operations (
GetCustomersByRank
/GetCustomersByCustomerId
) sort the entrie list before each query, which is very low efficient. The proper approach is to keep the customers in order, rather than sorting it everytime before each query.Here are some suggested approaches.
UpdateScore
GetCustomersByRank
GetCustomersByCustomerId
O(n)
O(n)
O(log(n))
O(n)
O(1)
O(log(n))
O(log(n))
O(log(n))
O(log(n))
The above table lists the expected time complexity for each approach. You may choose an approach to refactor your code.
Br.