DEV Community

Cover image for JS Coding Question #11: Are Two Object Equal (💥3 Solutions💥)
Let's Code
Let's Code

Posted on

JS Coding Question #11: Are Two Object Equal (💥3 Solutions💥)

...And the series continues after couple of React Interview Questions and Coding articles. 👍

Now, this interview question can be tricky as you would need to know how to recurse in order to solve this problem/challenge. Even seasoned engineers often stumble to answer this question so better not to underestimate this problem but be ready. Video format is available below if you don't feel like reading. Here is a Codepen if you want to edit/play around the code.

Interview Question #11:

Write a function or program that checks if two objects are equal.

There can be many solutions to problems/challenges that are efficient and less efficient, elegant and less elegant. If you have any other than the 3 solutions I have, please share so others may benefit. Below is my 3 solutions.

Solution #1:


const sortString = (str) => str.split("").sort().join("")

function isEqual(obj1, obj2) {
  const a = JSON.stringify(obj1);
  const b = JSON.stringify(obj2);

  // sort so it will handle object properties that are not in order
  return sortString(a) === sortString(b)
Enter fullscreen mode Exit fullscreen mode

This is an approach that looks very dirty/hacky but may still be useful if one is comparing smaller objects. It is easy and fast to write and do not need any libraries which adds overhead to the final js bundle. Since this is fast to write, it may also be useful for quick sanity check or verifying if two objects are equal.

Solution #2:

Using a lib

function lodashEqual(obj1, obj2) {
  return _.isEqual(obj1, obj2);
Enter fullscreen mode Exit fullscreen mode

Most codebases have js utility already. What I like about libraries are that they solve common problems effectively and are well-tested to cover edge cases. I love open source libs and I am firm believer not to reinvent the wheel for problems that are already been solved.

Solution #3:

Custom Approach

function deepEqual(obj1, obj2) {
  // ensure that arguments are objects
  return obj1 && obj2 && typeof obj1 === "object" && typeof obj2 === "object"
    ? // return false right away if objects properties length are not equal
      Object.keys(obj1).length === Object.keys(obj2).length &&
        // use reduce setting the initial value to equal which is true
        Object.keys(obj1).reduce((prev, curr) => {
          // then recurse as deep as possible and keep recursing if values are objects
          return prev && deepEqual(obj1[curr], obj2[curr]);
        }, true)
    : // just do normal compare if not an object
      obj1 === obj2;
Enter fullscreen mode Exit fullscreen mode

This custom approach requires recursion to be able to compare nested, deep objects. Many interviewers would want to see interviewees show/draft their own solution as they want to see how they communicate, think and code at the same time.

Final Thoughts:

If I am interviewing someone and they can tell me that solution maybe using a libraries AND sudo code a custom solution trying to recurse through it, I would be satisfied with those answer. How about you? What are your thoughts?

Feel free to bookmark 🔖 even if you don't need this for now. You may need to refresh/review down the road when it is time for you to look for a new role.

Top comments (0)