Monday, September 4, 2017

Implementing shared collaborative maps
Introduction: Maps such as Bing Maps or Google Maps helps us visualize our driving route and locations. when the same map is used to share relative locations of two or more cars plying on the road, it becomes a shared collaobartive map. This is like  a multiplayer boardgame where participants update their location against the same background in near real time while working their way to their destinations. if the destination is common to the multiple players, they can each see the distances and travel time for others.
Implementation: Such a shared collaborative map can be easily maintained with a shared database that can be updated individually by each participant. The scope and duration of the data persistence is only for the lifetime of the map. The data saved in the database only includes static information such as the origin destination tuple, the route information, the URI to query the location information for each participant based on their route map. In a sense it is nothing different from a shared collaborative online document such as Google docs or spreadsheets except that the updates happen automatically from each participant. Collaborative chat message board servers are great examples of the TCP ( though not absolutely necessary ) based implementations that show how collaboration is achieved in a multiple participant mode. The key differences are that the updates do not affect each other and are therefore already lock-free and the shared map shows arrows or pins for the locations of the cars against the same map. The dynamic location updates come from querying the location REST API from the individual participants. Layers can be drawn over the map to render one or more participants against the same backdrop just the same way as landmarks and traffic are annotated on the map.  Moreover, if an intial URI can be set up for all the shared metadata and static data for all the participants, then it becomes easy to even do away with the database and use a service only implementation.
Conclusion: Users may not always find this feature as a menu option on their maps application but the implementation is do-able with their programmatic APIs.


#codingexercise
Find the next larger element greater than twice itself for all in an integer array
Int[] GetNextLarger2XElements(List<int> A)
{
var result = new int[A.length];
for (int i =0; i < A.Length; i++)
{
int next = -1;
for (int j = i+1; j < A.Length; j++)
      if (A[j] > 2 x A[i]){
          next  = A[j];
          break;
      }
result[i] = next;
}
return result;

}
We could also  do this with the help of a stack which we keep for all the elements that do not have a next 2X larger element.
we push the first element in the stack. we pick the next item in the array if the next is smaller than the element in the stack, we print the tuple and pop the element otherwise we push it back on to the stack for retaining the elements we have not found an answer yet. we also push the next element on to the stack so it can participate for matches going forward. This is still O(N^2) but instead of looking ahead through all the elements we are looking back at the collection of unmatched so far. In the worst case, this stack will grow to be the length of the array. The order of the stack is the reverse order of the portion of the array we have covered.

No comments:

Post a Comment