Skip to content

Competitive-Coding-10 Completed! - #468

Open
hiteshleburu wants to merge 1 commit into
super30admin:masterfrom
hiteshleburu:master
Open

Competitive-Coding-10 Completed!#468
hiteshleburu wants to merge 1 commit into
super30admin:masterfrom
hiteshleburu:master

Conversation

@hiteshleburu

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Interview Problem : Buy and Sell Stock (buyAndSellStock.py)

Your solution is excellent! It efficiently solves the problem with optimal time and space complexity. The approach is straightforward: iterate through the array and add any positive difference between consecutive days to the profit. This works because you can buy and sell multiple times, and capturing every small increase leads to the same total profit as holding for a longer period (for example, buying at day 1 and selling at day 5 gives the same profit as buying day 1, selling day 2, buying day 2, selling day 3, etc., if prices are increasing every day).

One minor point: consider adding a comment explaining why this greedy approach works (e.g., "We can capture all positive daily price movements since multiple transactions are allowed"). This would make the code more understandable to others.

Also, ensure that the variable names are consistent and clear. Here, n is used for the length of prices, which is common, but you could also use len(prices) directly in the range if you prefer to avoid an extra variable.

Overall, great job! Your solution is efficient and correct.

VERDICT: PASS


Interview Problem: Peeking Iterator (peekingIterator.py)

Your solution is well-structured and handles the problem correctly for the given constraints. However, there is a potential issue with the hasNext method: it relies on the truthiness of nextEle. Since the problem states that integers are between 1 and 1000, there are no zeros, so it works. But if the iterator were to contain zeros (or other falsy values like False in a boolean iterator), your hasNext would incorrectly return False. To make the solution more robust and generic (as mentioned in the follow-up), you should explicitly check for None to indicate the absence of a next element.

Here's how you can improve:

  • Instead of setting nextEle to None when there are no more elements, you are already doing that in advance. But in hasNext, you should check if self.nextEle is not None instead of if self.nextEle. This ensures that even if the element is zero (or any falsy value), it will be correctly recognized as present.

Also, consider inlining the advance method in next for simplicity, as it is only called there. Alternatively, you can keep it as a helper if you prefer.

Revised hasNext:

def hasNext(self):
    return self.nextEle is not None

For the __init__ method, you can set self.nextEle = None initially and then update it only if the iterator has next. Alternatively, you can call self.advance() after setting self.iterator to initialize nextEle. Your current approach is correct.

Overall, your solution is good, but for genericity, you should avoid relying on truthiness of elements.

VERDICT: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants