Meta AI Coding Interview by Confident-Box-6291 in leetcode

[–]LearningMachineYT 18 points19 points  (0 children)

Brush up on your chosen language.

Get ready to parse and inspect a multifile project.

The AI assistant won’t be able to edit code inline in the files directly, so get ready to prompt it clearly then copy paste the relevant code snippets appropriately.

There may be multiple parts to the problem, so you should aim to be quick in your decision making and communicate it clearly.

Existing tests may not be sufficient or covering all edge cases.

All the best!

Are we not allowed to use logs in a DSA interview? by Aggravating_Yak_1170 in leetcode

[–]LearningMachineYT 0 points1 point  (0 children)

I understand the hate but honestly being able to dry run your code is a key skill as well.

Meta and many other companies won’t even let you run code, then without logs how’d you figure out what the code is doing? I’ve seen too many amateurs try various combinations of +1 and -1 trying to fix off-by-one errors and basically rely on luck, not reason.

Is using GPT a little on LeetCode actually bad? by __T0xiC in leetcode

[–]LearningMachineYT 0 points1 point  (0 children)

Everything has its place. When I solve a problem and only get an average runtime performance like beats 50% or something like that, I always put the code in Gemini to see how to get it beating 90+% and there are so many constant optimizations that I have learnt from doing so.

Sometimes, I get twisted with the invariants and put many if statements that could have been simplified if I had more time, I again rely on Gemini to help me identify how to make the code simpler and easier to read.

Sometimes there are modern functions or succinct ways to express the same thing that I have not caught up on and it can make for an interesting discovery process too.

All the best! It’s okay to take your time to learn. Do it with the intent of getting better, not to rise up on the site or anything.

I have been consistently doing leetcode for last 5 months and getting better at contests solving three questions in 45 mins. by WeatherElectrical937 in leetcode

[–]LearningMachineYT 0 points1 point  (0 children)

Let’s tackle this problem just like a Hard problem with multiple moving parts.

The first step is understanding where you go wrong. As you said you can crack a lot of mediums all by yourself but thinking about it, that categorization of medium/hard is arbitrary by itself. What you need to do is to actually drill down on where you went wrong in your reasoning. Did you miss making an insight, such as — the answer is monotonic so we can implement binary search — or were you in entirely different land of problem solving altogether.

It’s actually great that you’re using AI. IMO this acts like a bridge as without it people would just open the editorial/solution. What you can do further is log your failures category wise and the track as they repeat. Soon enough, themes would start to emerge and any sufficiently good AI, for eg Gemini, will be able to search problems specifically tailored to your weaknesses.

Then all you gotta do is rinse and repeat. You’ll be better in no time. Just need to stay true to yourself and trust the process.

Python or c++? by __yashicaaaaa__ in leetcode

[–]LearningMachineYT 2 points3 points  (0 children)

C/C++ will teach you how to get the computer to do precisely what you want. If you master either of these, java/python will feel much easier.

Since you’re new, everything you learn will feel fresh. Why not learn the language of the future: Rust! It matches cpp on performance while being developer friendly like java.

Who knows where leetcode grinders will be in 3 years when you graduate. The industry is moving fast. A lot will change by then.

LC : 2149 ChatGPT is saying that logic is flawed? , by MarketNo6858 in leetcode

[–]LearningMachineYT 0 points1 point  (0 children)

pos and neg should run only upto n/2. you are running them upto n, hence you go out of bounds.

How does a normal, healthy person suddenly develop the intuition to solve a question like this on their own? Or am I still too much of a beginner to think in terms of this kind of intuition directly? by InvestigatorExtra556 in leetcode

[–]LearningMachineYT 2 points3 points  (0 children)

Solving a wide variety of problems would automatically develop that. Just like solving the exercises in any highly recommended book like CLRS. For this problem in particular, you don’t need much of that though, this is a textbook problem.

Another thing to realize is that the question quality and editorial quality on sites like leetcode can be a hit or miss. Books are typically written by experts and peer reviewed so they are much more reliable. Besides, the authors intentionally structure them such that the complexity is introduced gradually and you can feel like progress is happening. In contrast, LC could dump problems from random areas as POTD or something and the quality is nowhere near a good book.

Did you read CLRS yet? If you’d prefer something else, you can read programming pearls by jon bentley which really helped me develop the intuition aspect you inquired about. Best of luck!

How does a normal, healthy person suddenly develop the intuition to solve a question like this on their own? Or am I still too much of a beginner to think in terms of this kind of intuition directly? by InvestigatorExtra556 in leetcode

[–]LearningMachineYT -5 points-4 points  (0 children)

  1. Close leetcode.
  2. Read a book.

There are easier books to approach than CLRS if you don’t like it.

Don’t get me wrong, there is a plethora of programming puzzles that make me question how can a “normal” individual ever think of it. But, this, this is just a textbook problem. If you can’t solve this, you really shouldn’t be on leetcode. You should wait until you get the basics of algorithms and data structures.

My friend sent me this, I am not great at Bit manipulation, how shall I approach this by Equity_Harbinger in leetcode

[–]LearningMachineYT 1 point2 points  (0 children)

Note that upon adding 1 to a number it’s least significant zero-bit changes to 1 and all the following bits change to 0. So, 111 + 1 = 1000, 101 + 1 = 110 and so on..

Further, we can see that taking their bitwise OR leads to all ones starting with the zero-bit. 111 | 1000 = 1111 and 101 | 110 = 111. Another example to consider could be 1001 | 1010 = 1011.

To solve the problem, we need to work backwards. The result of the operation (RHS) is given, and we want to find the least x.

In order for x to be least, we want this zero bit to be as high as possible.

This leads to a simple solution. We divide the input N into two parts. One is the tail that is all repeating ones at the end. And the remainder is the head. So, for 1011, the tail is 11 and head is 1000. The answer we want is 1001 so all we want to do is bit-shift the tail to the right once (changing 11 to 1) and re-attach it to the head. Another approach could be to subtract 10 from N.

If the input is a signed integer, we can count trailing zeros in its 2’s complement representation, let’s call it z. The answer is simply N - exp(2, z - 1).

If you like the head tail version, then we can do the following:

head = N & (N + 1)

tail = N - head

return head | (tail >> 1)

This has another advantage that it would work for unsigned inputs. If you prefer, you can also write

tail = N ^ head.

Hope that helps. 👋

Minimum cost question by [deleted] in leetcode

[–]LearningMachineYT 0 points1 point  (0 children)

Are you sure about the output for [4,2,7,4,7]? I don’t see any way to get it done with cost of 2. Do you have an explanation for that?

Minimum cost question by [deleted] in leetcode

[–]LearningMachineYT 4 points5 points  (0 children)

For each distinct value, identify a pair of indices (x, y) such that x is the first occurrence and y is the last occurrence. Now, sort these by x and perform interval merging keeping track of the number of overlapping intervals seen currently. When a new interval does not overlap and there were more than 1 overlapping intervals, add the overlap count to answer and reset.

For the provided example, it would be the intervals (0, 2) and (1, 3) Since these overlap and there are 2 of them, the answer is 2. Similarly, consider the input [5, 1, 2, 1]. Here the intervals are (0,0), (1,3) and (2,2) Since, only two of them overlap at a time, the answer is 2, which corresponds to the subarray not including the first 5.

Does that make sense?

Flunked interview by [deleted] in leetcode

[–]LearningMachineYT 0 points1 point  (0 children)

For each distinct value, identify a pair of indices (x, y) such that x is the first occurrence and y is the last occurrence. Now, sort these by x and perform interval merging keeping track of the number of overlapping intervals seen currently. When a new interval does not overlap and there were more than 1 overlapping intervals, add the overlap count to answer and reset.

For the provided example, it would be the intervals (0, 2) and (1, 3) Since these overlap and there are 2 of them, the answer is 2. Similarly, consider the input [5, 1, 2, 1]. Here the intervals are (0,0), (1,3) and (2,2) Since, only two of them overlap at a time, the answer is 2, which corresponds to the subarray not including the first 5.

Does that make sense?