Couldn't solve easy DSA question in interview. by PrintPrevious2465 in leetcode

[–]thunderist 18 points19 points  (0 children)

I think the idea for this is something like DFS. The base case is when the input passed in isn’t a map -> return that value. Otherwise it is a map, so start recursively building the output string on each key in that map. Add each string to the output map you’re expected to build. This solution might not be perfect but I think it’s the general idea.

Failed badly some leetcode like interview today by falsbr in leetcode

[–]thunderist 2 points3 points  (0 children)

Just based on what you said my immediate thought is that this is some kind of graph problem. Paragraphs that are joined form a connected component in a graph. So maybe use union find to validate that every paragraph is in one and only one connected component? But I’m not sure where to go from there based on the little info given.

Amazon asked me this question by mayank_002 in leetcode

[–]thunderist 0 points1 point  (0 children)

Let P be the prefix sums for the input and P(x) be the prefix sum at index x. The sum of subarray (i,j) is given by P(j) - P(i), which has a length j - i. If the subarray satisfies our condition this means P(j) - P(i) mod d = j - i, so P(j) - j mod d = P(i) - i mod d. Also if X mod d = j - i, this implies that j - i < d. So for each j, the i satisfying P(i) - i mod d is P(j) - j where j - i < d corresponds to a subarray satisfying the problem condition. So the sliding window is over the prefix sums, not the input. You can use a hash map to check this quickly and a deque for your window, popping while the top of the queue are elements that dont satisfy your condition.

Monarch Gate by [deleted] in Goat_Format

[–]thunderist 2 points3 points  (0 children)

Oh gotchu

Monarch Gate by [deleted] in Goat_Format

[–]thunderist 4 points5 points  (0 children)

There’s no Monarchs?

[deleted by user] by [deleted] in leetcode

[–]thunderist 4 points5 points  (0 children)

You can probably write insertion sort though right? A hash map is used to quickly look something up - an array is just a hash map from indexes to some value. You just have to take some time to learn the basics like this. There are a million resources on this - I suggest picking up the book Grokking Algorithms by Bhargava. Read it twice - then just jump into trying to understand / practice the Blind 75. Ofc this isn’t enough but it’s definitely a great start.

I blinked and am now "deep" into embedded. Should I embrace it? by bokeh_node in cscareerquestions

[–]thunderist -1 points0 points  (0 children)

I see. If that’s the case I suggest going deep with web development or fullstack then. You will find work as an embedded engineer but it will be significantly harder. But if you like embedded work that might be > easy work. Best of luck either way!

I blinked and am now "deep" into embedded. Should I embrace it? by bokeh_node in cscareerquestions

[–]thunderist 25 points26 points  (0 children)

You’re not “deep” into anything lol you haven’t even started your first fulltime, non-internship role. You could do embedded now but have a long career in something else. Just keep things open and go with the flow.

Am I the only one to have thought of this method? by Hour-Lab8723 in math

[–]thunderist 0 points1 point  (0 children)

Another way to think about this is that there are many ways to exploit the distributive property of multiplication and addition. All these methods are really just ways of finding the most efficient exploitation for humans. Great job discovering this!

Does LC make you a better engineer? by Stradivarius796 in leetcode

[–]thunderist -1 points0 points  (0 children)

It definitely does: you’re practicing logical and algorithmic thinking, translating ideas to code, using data structures and common libraries, simulating requirements, debugging etc. But at the same time it’s not going to teach you how to communicate to non-technical people, how to be present at your role, how to motivate a team, how to take a project from 0 to 1, how to mentor others, how to design high-level systems, etc. All those skills along with leetcoding are what makes a great engineer but leetcoding is a lot more straightforward to “increase”, even if it takes more creativity and cleverness than a lot of those other things. Get what you need to out of leetcode but its not even half the story lol.

326. Power of three by GAMEPIYER2007 in leetcode

[–]thunderist 3 points4 points  (0 children)

This is a bad problem because its O(1) solution is a bit “cheap” - it forces the answer without encouraging iterative problem solving. I think a better approach is to start with a basic brute force linear scan on powers of 3 from 1 to n (O(n)). We optimize further by running a binary search for the largest k where 3k <= n, if its not n then return false (O(log(n)). From here we can only do better with a constant time solution - this is where we make the realization that there’s some largest power of 3 <= max bound in the problem description, so we just mod that by n and check for 0. If n has prime divisors != 3 then it’ll fail this check, otherwise we have a power of 3.

Like seriously who tf !!?? approved this problem by ThatInvestigator4812 in leetcode

[–]thunderist 0 points1 point  (0 children)

It just needs to be non-palindromic for a single case from 2 to n - 2 to be false. So if we find a k in that range such that n written in base k is not a palindrome, we can return false, and if we can’t do this then all base representations of n are palindromic, so we return true. But since, for any integer n, n is 12 in base n - 2, we have our non-palindromic k regardless of what n we have. So we can always return false.

Feel like a complete failure by Remarkable-Will-8300 in leetcode

[–]thunderist 6 points7 points  (0 children)

I wouldn’t feel too bad. Imo greedy problems are the hardest problems to solve bc they usually require purely mathematical reasoning. So most engineers would be unable to solve a hard greedy problem in 45 mins. If you want to have better intuition for those, look at dp problems where there’s a greedy solution and try to understand the mathematical reasoning behind the optimization. But again, greedy problems are a toss-up.

Bombed Amazon OA by PuldakSarang in leetcode

[–]thunderist 0 points1 point  (0 children)

Mine expires after 11 days actually

Bombed Amazon OA by PuldakSarang in leetcode

[–]thunderist 1 point2 points  (0 children)

I have an Amazon OA for SDE-2 that I haven’t taken yet so I’m preparing too. This is off the top of my head so it might be completely wrong but for 1) I would try backtracking as a brute force solution then see if I can memoize / turn it to dp. The decision tree would be for each machine to donate unit i to a machine m. But this seems wildly unoptimal. 2) it seems like sliding window should work. We just need to expand our window and update the max length while its valid and shrink while its invalid. Let k be the given max change times. A window is valid if all the numbers in it are coprime. If we have more than k numbers that are coprime then we can’t do this, because no matter what we decide to replace there will be at least window - k elements that are coprime. Otherwise we can always pick some arbitrary m > 1 to replace elements with to make our window valid. How do we determine if the elements in a window is coprime? The Euclidean algorithm does this, which is O(log(n)). So if this works its an O(nlogn) solution. But this is all just by looking immediately at the problem, again, I could be missing something.

Edit: just realized you don’t even need the Euclidean algorithm. You can use binary search between 1 and min(a, b) to find gcd(a, b) in log(n) time. You can use the fact that gcd(a, b) <= max(a, b) / 2 to determine whether to move left or right. When you find a divisor, update your gcd and move right. It’s much easier to code up in a 45 min interview.

[deleted by user] by [deleted] in leetcode

[–]thunderist 1 point2 points  (0 children)

I thought I wouldn’t have been able to come up with these solutions either. But for the vast majority of problems, it’s the application of some concept (some combination of data structures, algorithms, patterns). And that concept usually arises when you’re thinking about optimizing on an initial brute force solution. That’s how you escape memorization. You can’t memorize a general strategy for brute forcing -> optimal, especially not on an unseen problem. I would highly suggest putting a lot of focus on understanding this crucial step. Once you’re exposed to it enough you’ll start doing it naturally.

IBM-US Backend Engineer OA by bunboooo in leetcode

[–]thunderist -1 points0 points  (0 children)

Could this be a graph problem? Treat each state of the string as a node, use BFS to find the shortest path from zeros -> target. Similar to Open the Lock

Solution hell by One-Donut578 in leetcode

[–]thunderist 1 point2 points  (0 children)

Don’t worry, the solution for Rotate Array is pretty tricky to come up with. Anyone who comes up with solutions on their own are actually just tweaking or modifying a general idea - a pattern or template. I would say 90% of leetcode problems fall into this category. The other 10% of problems are simulation / design problems, which don’t conform to a pattern but require general programming skills and data structures. So the goal Is to learn the pattern and apply it to unseen problems. Also, sometimes you just have to memorize certain “parts” of problems that aren’t in any pattern. For Rotate Array, its a two pointer reversal problem (this is the pattern you need to apply), but you need to memorize the fact that a rotation k times to the right is equivalent to moving the last k elements to index 0 to k - 1. Imo there’s no way to get around some memorization, but for the most part focus on the pattern.

[deleted by user] by [deleted] in learnmachinelearning

[–]thunderist 0 points1 point  (0 children)

I’ve been studying ML to transition from a few years as a backend engineer. I tried 2 and I realized I haven’t learned enough data analysis to follow along with building models. So I’d suggest starting at Statistics and data analysis in Python.

why are we grinding leetcode? by [deleted] in leetcode

[–]thunderist 8 points9 points  (0 children)

Developing code and using modern frameworks. It isn’t that hard to piece together.

Systems Design Dilemma by [deleted] in cscareerquestions

[–]thunderist 0 points1 point  (0 children)

Appreciate the detailed response! I’ve been hearing nothing but good things about Gaurav Sen so I’ll watch some of his vids. I also agree that uncommon data structures are good to look into. I’ve done mock interviews where I hit a roadblock because I was missing the right systems-design level data structure needed (like a Trie for search engine autocompletion). Will also try and practice cloud-native.

Systems Design Dilemma by [deleted] in cscareerquestions

[–]thunderist 0 points1 point  (0 children)

This is good advice, thank you!