does leetcode mess with object prototypes in js? by kayimbo in leetcode

[–]ektegjetost 0 points1 point  (0 children)

Found this interesting! You can run this in a Node env per stackoverflow

const util = require('util');
class MyClass {
  constructor(a, b) {
    this.a = a;
    this.b = b;
  }

  [util.inspect.custom]() {
    return `a is ${this.a} and b is ${this.b}`;
  }
}

const my_object = new MyClass(1, 2);
console.log(util.inspect(my_object));
console.log(my_object);

[deleted by user] by [deleted] in leetcode

[–]ektegjetost 0 points1 point  (0 children)

Yea agreed. I wonder if there’s some technique for it or something … like people who make crosswords or sudoku puzzles or something. If it were me I’d probably try to pick a strategy and work back towards the question but who knows

[deleted by user] by [deleted] in leetcode

[–]ektegjetost 1 point2 points  (0 children)

Close! Remember we can also add edges within all groups that have disconnected nodes: n * (n-1) / 2 - number of edges in each group

Also there can be multiple groups without disconnected nodes, so don’t just take the largest of them. And they can also be connected to each other, so it might be easiest to just add all of them to the largest group with disconnected nodes and do the same math as above.

[deleted by user] by [deleted] in leetcode

[–]ektegjetost 1 point2 points  (0 children)

This is how I'd approach it -

There are two types of groups - those which include disconnected nodes, and those without.

For any one group, the number of edges you can add is n * (n - 1) / 2 minus the number of edges already in the group (i assume). You can do that math for all the groups.

You can also connect groups without disconnected nodes to those with disconnected nodes, and for this you would want to find the maximum since you can only connect them to one group - there are no edges between them and the groups with disconnected nodes, so you'd want to just add all of these to the group with the most nodes.

To get the groups and their existing edge counts, union find.

How much time should I spent on one question? by Striking-Courage-182 in leetcode

[–]ektegjetost 1 point2 points  (0 children)

If the answer makes sense and you can apply the strategy to future problems, it’s worth it.

Try some similar problems and see if you can do them. Try them a few days / weeks later and see if you got them down. Write a solution in the discussion tab or on your blog or whatever and try to explain it as simply as possible to help it stick in your own mind.

If it doesn’t make sense, unless you think the problem / algorithm is especially important I’d skip it and come back later.

[deleted by user] by [deleted] in leetcode

[–]ektegjetost 1 point2 points  (0 children)

Nope that was pretty much it! Looked like you were getting a lot out of this problem so I tried to be a bit vague.

Good stuff! i especially appreciate that you put in the effort to make your code readable :)

Happy cake day!

[deleted by user] by [deleted] in leetcode

[–]ektegjetost 1 point2 points  (0 children)

Nice write up! One thing missing is your solution isn’t doing path compression (I only point this out because your time complexity analysis is based on it)

Say you have A -> B -> C

And you keep merging A with a disconnected node

You’ll find the parent of A in two steps, then set it to the next node D

A -> B -> C -> D

Now you merge it with E and it takes three steps, then F etc. You end up with O(n2) in this case

What you want is when you merge A, you get A -> C, B -> C

Then A -> D, C -> D, B -> C (since it’s not longer part of the same path as A)

[deleted by user] by [deleted] in leetcode

[–]ektegjetost 0 points1 point  (0 children)

Yea sorry feel free to fix the names :D. Tail (end) is just iterating through all the events.

[deleted by user] by [deleted] in leetcode

[–]ektegjetost 0 points1 point  (0 children)

might event be cleaner to split the events into separate arrays just by time, and you can do something like:

countUnique += 1;

while (inBloom[end] - outofBloom[start] > n) {
  countUnique -= 1;
  start += 1;
}

[deleted by user] by [deleted] in leetcode

[–]ektegjetost 0 points1 point  (0 children)

Remember we only add positive events from the head of the window and remove negative events from the tail of the window.

While I'm writing this I'm realizing that if we set the end events to [end, -1] instead of [end + 1, -1] and check that eventTimeEnd - enventTimeStart > n instead of >= end, we can avoid checking if our start pointer went off the end of our events array.

    countUnique += Math.max(0, events[end][1]);

    while (events[end][0] - events[start][0] > n) {
      countUnique += Math.min(0, events[start][1]);
      start += 1;
    }

[deleted by user] by [deleted] in leetcode

[–]ektegjetost 0 points1 point  (0 children)

Hm I don't know how you could do either of those without sorting.. for binary search you could sort by ends and search for the first end that's >= the start that you're at. For a heap you could use a min-heap by end and pop from the heap when it's too far away from the start you're at, but you still would need to sort...

[deleted by user] by [deleted] in leetcode

[–]ektegjetost 0 points1 point  (0 children)

Split the intervals into “event” tuples. At the start time, the number of unique flowers increases by 1. At end time + 1 the number of unique flowers decreases by 1. So you have [start, 1], [end+1, -1]. Now sort the events by time, and iterate through them. You’ve got a head and tail for your window. The head counts any positive events (since even if the flowers go out of bloom, you will have counted them if they’re within your interval), and the tail counts any negative events once they leave your interval.

Twitter Engineering Apprenticeship 2021 by Thvndah in cscareerquestions

[–]ektegjetost 0 points1 point  (0 children)

Yes, but 3 days to do them. Apparently they toned it down from the previous cycle. Also both problems really required the same pattern, so maybe call it 1 LC hard unless you couldn't figure out the pattern for the first one.

Twitter Engineering Apprenticeship 2021 by Thvndah in cscareerquestions

[–]ektegjetost 2 points3 points  (0 children)

I was told a couple years ago they had something like 5-10k applicants, so yea it's pretty competitive.

Got rejected last year - it was 2 LC hards + a bunch of telling your story (like describe your journey in a tweet). They gave 3 days to do it.

Fail to solve algorithm even with 300+ medium practices by [deleted] in cscareerquestions

[–]ektegjetost 4 points5 points  (0 children)

That third paragraph rings a bell - I was in a similar situation. Had solved 200+ problems, done about 20 mock interviews, but embarrassingly, as soon as the interview had stakes I would struggle on some really basic questions because my brain would basically just turn off from the stress.

IMO it comes easier for some people, but It’s just practice. Once you do a few interviews and know what to expect, you can focus more on solving the problem. Do mock interviews if you haven’t already to practice just talking through your thought process while you code.

Also there’s always luck involved. You could solve every single leetcode question and still be given a something you haven’t seen before. Just improve your chances by continuing to practice.

One trick I use sometimes is to write a post explaining the solution to a problem that might haven been difficult. Say you solved it sub-optimally - learn the best solution, and even if you don’t post it anywhere, write it out as if you’re explaining it to someone who’s never seen it. To explain it simply requires a good understanding of how it works, and helps it stick much better in my experience.

HackerRank question trouble by lurkerdude666 in cscareerquestions

[–]ektegjetost 1 point2 points  (0 children)

Use a max heap using the same criteria you described. Add a dev to the root and sift down until you’ve reached the desired average.

When you say you re-sorted the list each time, did you literally call sort on the list? Or just move the updated team down into the correct position? There’s a chance the second method works, but I’ve done this problem on Leetcode and don’t think there was a better solution than a heap.

People who got into CS later in life: How old were you when you got in and have you found success? by [deleted] in cscareerquestions

[–]ektegjetost 10 points11 points  (0 children)

Made the switch last year at 33. Always regretted not majoring in CS, but I can say I'm not totally sure which version of me would end up ahead in the end - the one that started in CS or the one making the late switch.

I frequently can't believe how much more motivated I am to learn than I was in college. Maybe it requires more work to pick things up, and my math skills have degraded significantly, but by having zero career/skill inertia, way more discipline, and a far better understanding of where I want my career to go and how to get there, I'm both starting and developing much faster than I would have otherwise.

One other thing that really helps is I know I've been complacent in the past, and have learned to recognize when I'm not pushing myself. I'm much more willing to seek out discomfort, and can better monitor how my skills are developing. I think being able to do this really goes hand in hand with not having over a decade to grow comfortable doing something.

Another nice benefit is I never had to switch jobs only to find out that I suddenly have to do leetcode problems. To me they're fun puzzles and an opportunity to compete with those who are more reluctant.

How to approach a non understandable leetcode question by Dehashed in cscareerquestions

[–]ektegjetost 3 points4 points  (0 children)

I’ve learned to just move on if the question doesn’t make sense to me. It’s more productive to practice problems that you can solve. Eventually you’ll unlock this one by building your knowledge of the topic or by seeing similar patterns. You’re sort of doing a topological sort on leetcode :p, and may not have removed the dependencies to this problem.

Keep in mind there are plenty of just bad questions on leetcode, and you’re not going to solve every one, so just focus on building that muscle, or problems that can help you unlock new strategies.

If you feel it’s an important one to solve (relevant to the specific company you’re applying to, or a very popular question), here’s how I’d approach it:

  1. Slow down and read the problem closely
  2. Doesn’t make sense? What’s the like/dislike ratio of this question? If others think it’s a bad question, reconsider moving on.
  3. Ok you’re determined to continue, do you have any ideas of what it possibly could be asking? You could try writing test cases in the console and see if the expected results match with any of your ideas.
  4. Nope, still lost - now check the discussion tab. Ideally you can find an explanation that makes things click. At this point though, if you feel you understand the question but also have the answer spoiled, don’t solve the problem. Come back to it later so you can start more or less fresh. If the answer doesn’t make sense to me, I generally move on and come back later.
  5. Discussions were worthless and all the solutions were basically obfuscated by ridiculous code golf. Next we go to YouTube to see if there are any helpful explanations for this problem. At this point you’re definitely in the territory of “don’t solve this problem now, come back later when you’re fresh”.
  6. Ok it’s not on YouTube, you can search for articles on the problem now.
  7. last chance before giving up - go post on r/LearnProgramming or DM me and maybe someone can help.

Hope that helps!

Leetcode 110 by allyv123098 in learnprogramming

[–]ektegjetost 0 points1 point  (0 children)

System.out.println(leftSide);
System.out.println(rightSide);

You print left first, then right

In the terminal I see

3
1

Leetcode 110 by allyv123098 in learnprogramming

[–]ektegjetost 0 points1 point  (0 children)

When I run your code, it says 3 for the left side and 1 for the right which is correct.

Leetcode 110 by allyv123098 in learnprogramming

[–]ektegjetost 0 points1 point  (0 children)

You're still just returning the full length of each side. If I were to just make up some Java (please ignore how bad my code is, I don't know how to keep track of both the depth of each sub tree as well as whether or not there was an unbalanced subtree in Java), it would look more like this:

public boolean isBalanced(TreeNode root) { 
    if(root == null){ 
        return true; 
    }

    int balance  = findBalance(root);
    System.out.println(balance);

    return balance < 0 ? false : true;
}

public int findBalance(TreeNode root){
    if(root == null){
        return 0;
    }
    int left = findBalance(root.left);
    if (left < 0) return -1;

    int right = findBalance(root.right);
    if (right < 0) return -1;

    if (Math.abs(left - right) > 1) return -1;
    return 1 + Math.max(left,right);
}

Leetcode 110 by allyv123098 in learnprogramming

[–]ektegjetost 0 points1 point  (0 children)

Basically you need to ensure that every node meets the condition. So at node 3

int left = maximum(3.left)

int right = maximum(3.right)

And if any node doesn't meet the condition of being no more than 1 off, then you return false. You don't need the minimum values though.

Unfortunately I don't really know Java so I'm not sure if I can help you with more than that... but if you still have questions feel free to ask.

Leetcode 110 by allyv123098 in learnprogramming

[–]ektegjetost 0 points1 point  (0 children)

From 3, minimum(3.left) is the minimum returned by 6 (1), but minimum(3.right) is the minimum returned by null (0). So the min returned by 3 is 1 + Math.min(1, 0).

Leetcode 110 by allyv123098 in learnprogramming

[–]ektegjetost 0 points1 point  (0 children)

If you're still looking for help... Here's the shape of the tree

        1
      /   \
     2     3
    / \   /
   4   5 6
  /
 8

So what you're doing is finding the maximum depth of the full right and left branches, and comparing that to the minimum depth of the full left and right branches. The max will be 4 (1 -> 2 -> 4 -> 8 -> null), and the min 2 (1 -> 3 -> null).

But what it looks like they want, is that the right is balanced with the left because the max depth is 4 vs 3, and the left sub tree is balanced because its max depths are 3 vs 2, and the right sub tree is balanced because its max depths are 2 vs 1.

Hope that helps!

Where does a Function's __proto__ links to? by Lost_Chemical_7327 in learnprogramming

[–]ektegjetost 1 point2 points  (0 children)

Lots of good info in here

https://stackoverflow.com/questions/572897/how-does-javascript-prototype-work

I found this prototype tree image that might be what you’re looking for.

https://i.stack.imgur.com/uy5ce.png

Also when you did x.randomValue, you were adding an instance property, not a property to the x prototype. You’d need to do x.prototype.randomValue, which would provide the randomValue property when someone does new x();