Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

75
Views
Using a HashMap for quickly looking up relationships between two entities

Tools used

Assume the language is JavaScript here, and the implementation of the HashMap is the Map class.

Data structure

Say I have a bunch of "People" objects that look similar to these two (which we will focus on), objects "John" and "Sally".

{
    id: "someUUID-John",
    name: "John"
}
{
    id: "someUUID-Sally",
    name: "Sally"
}

I also have n number of objects representing relationships between the above objects like so:

{
    id: "someRelationshipId",
    from: "someUUID-John",
    to: "someUUID-Sally",
    type: "phone_call"
}

The above example defines a relationship from John to Sally. Assume that there are multiple relationships going this same direction, and multiple going the other way (i.e. from Sally to John). There are also multiple relationships between other "People" objects and between both John and Sally to those other People.

Problem statement

The id of the above relationship is what this question is about.

I wish to use a HashMap to easily look up relationships. At the time I wish to look this up, it's safe to assume one of the two following scenarios:

  • I have a reference to John or Sally, but not both
  • I have a reference to both John and Sally

My question is, A) Is it possible to construct or use a hashing algorithm to generate a key that can be used to look up the relationship given a reference or both of the related objects and B) If so, what algorithm should I use or implement?

7 months ago ยท Juan Pablo Isaza
Answer question
Find remote jobs