How to use segments key-value datastore to find source and destination

Upon a search request, the source and destination addresses are translated into latitude and longitude values. With a key-value data store where the key is segmentId and value is coordinates[], how do you search for a segment that contains a particular lat/lon value?


Course: https://www.educative.io/collection/10370001/4941429335392256
Lesson: https://www.educative.io/collection/page/10370001/4941429335392256/6026783136940032

Hi Reza_Asadollahi,

You are right. With a key-value data store where the key is segmentID and the value is coordinates[], we can’t search for a segment that contains a particular lat/lon based on the key (segmentID), because that is exactly what we are searching for.

We can write a hash function that maps all the lat/long boundary coordinates for each segment to the respective segmentIDs and use that function to find segmentID for the given lat/long coordinates. For example, in the following key-value store


If we have to find ID for value 5, we can use the following function

def findID(value)
{
return floor(value/10)+1
}

// (5/10) + 1 = 0 + 1 = 1

Above is the simplest example. But we can fit polynomials to our data points (lat/long range for a segment, segmentID) and use that polynomial to find segmentID for a given lat/long. Though, there would be complex mathematical computation :slight_smile:

1 Like