Комментарии:
Object.prototype.toString.call(obj) === "[object Object]" is what I use to determine if an element is an object... its on the cusp of being hacky but it does work
ОтветитьYeah I’d fail after the first question which pisses me off because it just shows I’ll never pass an interview. The interviewer could always just hit me with something crazy and fail me
ОтветитьWhy didn't he use promise allsettled method in the first exercise?
ОтветитьAs a 25 year old who just started learning to code, this made my brain hurt 😂
ОтветитьWould you like me to help
You get rid of your lisp ?
Thank you for the video, I learnt a lot. However, I think there is an edge case for the second question,
lets say :
const obj1 = { a: undefined , b: 2}
const obj2 = { b: 2 , a: undefined }
with this code the output of
deepEquals (obj1 , obj2 ) will be false. whereas it should be "true"
I think instead of deepEquals (valueOneKeys, valueTwoKeys) in line 31, a better approach would be to iterate through valueOneKeys and if the item was not found in the valueTwoKeys then return false.
for (let i=0; i < valueOneKeys.length ; i++){
if ( valueTwoKeys.findIndex (element => valueOneKeys[i] === element) === -1 ) return false
}
function deepEquals(x, y) {
// All primitives, all same object references.
if (x !== null && y != null && x === y) return true;
// Edge case for primitives : Null
if (x === null && y === null) return true;
// Edge case for primitives : NaN
let xIsNan = Number.isNaN(x);
let yIsNan = Number.isNaN(y);
if (xIsNan && yIsNan) return true;
// Edge case for objects : One is array, make sure the other is array as well.
if ((Array.isArray(x) && !Array.isArray(y))
|| (!Array.isArray(x) && Array.isArray(y))) {
return false;
}
// Edge case for objects: Date
if(x instanceof Date && y instanceof Date){
return x.getTime() === y.getTime();
}
// Objects
if (typeof x === 'object' && x !== null && typeof y === 'object' && y != null) {
const xKeys = Object.keys(x);
const yKeys = Object.keys(y);
if (xKeys.length == yKeys.length) {
for (key of xKeys) {
if (!deepEquals(x[key], y[key])) {
return false;
}
}
return true;
}
}
return false;
}
deepEquals could work with a one-liner
return JSON.stringify(valueA) === JSON.stringify(valueB);
Wouldn't that be an acceptable solution? Is there a catch to that? I've also checked with all the test cases and it works for me.
They take you to the technical interview in the first meeting or they do that after the first meeting? I have a meeting soon but the recruiter didn't mention anything about technical interviews etc, they said they wanna meet me.
ОтветитьAs a Js - React developer is it required Hard DSA to get a job in google ?
ОтветитьGreat video, terrible language. Javascript is absolute asscheeks
ОтветитьFor the 1st question you can actually use async await in a for...of loop, because all promises are in the pending state, doing that if the first one takes longer, the others will be completed when you reach them in the loop.
ОтветитьYou guys forgot the case of either object being a function in deepEquals
ОтветитьThat was sick.. I didn't realize Clement was so good holy jesus
ОтветитьWhere we will get that job vacancy as react fe in google ? Ive never found job as fe in google
ОтветитьI think deepEquals function would fail one edge case by doing the last change if
obj1 = {b: 2, a: 1};
obj2 = {a: 1, b: 2};
because Object.keys() return the keys in insertion order and upon calling deepEquals again, it would be ['a', 'b'] and ['b', 'a'] and it would get failed while checking if 2 arrays are equal or not and in actual library it would return true.
oh god. weird js for ever😅
ОтветитьJust to add Object.entries() one can rely on the array it's return...I read about it just now while watching the interview ...Welldone guys....Super....
Ответитьwhy not ”With A Frontend Expert“? maybe you think he is not so good?
ОтветитьWhy cant we convert non primitives to string and compare those strings
ОтветитьJust a FYI. You dont have to use the name 'Google' for every mock interview just to attract viewers. Keep your content quality good and you will get viewers. You seem to be too much obsessed with Google, Facebook, Amazon.
Ответитьslow down when u talk
Ответитьvery good content
ОтветитьHey Clement! Wanted to ask about the Big O for time for deepEquals, is that O(n) right?
I'm currently taking a course for algo's and data structures and just wanted to ask you thattt
var assert = require('assert')
const obj1 = {a: 1, b: 2}
const obj2 = {b: 2, a: 1}
assert.deepEqual(obj1, obj2) // passes
assert.deepEqual(Object.keys(obj1), Object.keys(obj2)) // fails
I think you missed a case in solution 2
I think the second one we can simplify to use JSON.stringify two objects and compare two string
ОтветитьI just realized that coding logic is so hard because the interviewer also has to understand what the guy is doing 🤭 when he asks him questions and then he says, “oh okay I see!”
ОтветитьI did not think this video would give me so much, cause I am not so experienced in JS, but I actually understand how the functions works, and know the functions we use. So its fun to follow with this, and also learn what these functions do :)
ОтветитьDidn't you guys say that you can't rely on the order of Object.keys? Then calling deepEquals on the key arrays isn't a proper fix for the issue.
Ответитьyou could have used lodash's _.isEqual(val1, val2);
ОтветитьYou forgot to sort the keys before passing them to deep equals.
ОтветитьFront end and engineer... find the mistake
ОтветитьWonderful interview. @Corner Ardman I think one edge case is missed in PromiseAll problem , when you don't pass any promise to PromiseAll function.
Ответитьamazing your content.
Ответитьconst deepEqual = (a,b) =>
(typeof a != typeof b || (a==null) !== (b==null)) ? ((a,b) => {
if(typeof a === "object" && a){
let keys = Object.keys(a)
if(keys.length != Object.keys(b).length) return (() => false)()
for(let i = 0; i < keys.length ; i++)
if(b[keys[i]] !== a[keys[i]]) return (() => false)()
return (() => true)()
} return (() => false)()
})() : ((a,b) => a === b)()
It will probably break if an object member is a reference to another object, but I don't care, close enough
edit: it's wrong but it's cute
edit2: now it works kind of
edit3: now it's more better and more improved
edit4: now it's more functional react js style with no impurities
let a = { x : 33, y : 44}
let b = { y : 44, x : 33}
console.log(a === b)
console.log(JSON.stringify(a) === JSON.stringify(b))
b = { x : 33, y : 44}
console.log(a === b)
console.log(JSON.stringify(a) === JSON.stringify(b))
Conner has an annoying voice, sorry but I gotta say it. Try to speak a bit deeper so you don't sound like a preteen. Other than that good stuff
Ответитьthis just proves how huge mess the javascript is.
ОтветитьI decided to try it too 😅
function deepEquals(valueOne, valueTwo) {
if (valueOne === valueTwo) {
return true;
}
const isObject = (obj) => obj !== null && typeof obj === 'object';
if (isObject(valueOne) && isObject(valueTwo)) {
const keys = [...Object.keys(valueOne), ...Object.keys(valueTwo)];
return keys.every((key: string) => deepEquals(valueOne[key], valueTwo[key]));
} else if (Array.isArray(valueOne) && Array.isArray(valueTwo)) {
if (valueOne.length !== valueTwo.length) {
return false;
}
return valueOne.every((value, index) => deepEquals(valueOne[index], valueTwo[index]));
} else if (typeof valueOne === 'function' && typeof valueTwo === 'function') {
return valueOne.toString() === valueTwo.toString();
}
return valueOne === valueTwo || Number.isNaN(valueOne) && Number.isNaN(valueTwo);
}
deepEqual function, simply
function deepEqual(v1: any, v2: any): boolean {
if(typeof v1 === "object" && typeof v2 === "object")
return JSON.stringify(v1) === JSON.stringify(v2)
if(typeof v1 === 'function' && typeof v2 === 'function')
return v1.toString() === v2.toString()
return v1 === v2;
}
this is a fancy version of it
function deepEqual(v1: any, v2: any): boolean {
return (
typeof v1 === 'object' && typeof v1 === 'object'
)
?
JSON.stringify(v1) === JSON.stringify(v2)
:
(
typeof v1 === 'function' && typeof v2 === 'function'
)
?
v1.toString() === v2.toString()
:
v1 === v2
}
Serious question:
Why is this not appropriate for deepEquals?
const deepEquals = (object1, object2) => JSON.stringify(object1) == JSON.stringify(object2);
One more condition is missing, if array is not sorted and have random similar numbers like v1= [2, 4, 6] v2= [4, 2, 6]. In that case this is going to fail.
ОтветитьMan this guy is so weird hahaha hahaha hahaha lol
Ответить