Answer the question
In order to leave comments, you need to log in
Algorithmic problem for searching by a composite key
Good afternoon.
While writing the project, a question arose that can be solved in a lot of ways and, in principle, anyone will work, since the amount of data is small, but still there is a feeling that I lack knowledge of algorithms.
We have approximately the problem of such a plan. There are two users. One invites the second to a communication session. The second receives a notification about this, to which he can respond within, say, 1 minute. On the other hand, the second user can make a counter call and it will have the same result as consent.
What is there then.
user1.id, user2.id and timestamp at the moment of initiation of the invite, let's call it time.
You need to be able to search by user1.id AND user2.id regardless of the order of these arguments. The first thing that comes to mind is something like a key, the value of which is formed from user1.id + user2.id, regardless of the order of the terms. If you do a bitwise addition of id values, then everything seems to turn out cool, but there is a risk of getting a collision. It is already trivial to check the relevance of the offer further by taking the value from the array according to the found key.
I know this can be solved through two arrays or even a DB, but that's not interesting.
You can do symbolic addition of identifiers with a separator and sort through two options for comparison at each iteration, but is there really no better solution?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question