Skip to content

working solution - #469

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

working solution#469
avcode3 wants to merge 1 commit into
super30admin:masterfrom
avcode3:master

Conversation

@avcode3

@avcode3 avcode3 commented Apr 14, 2026

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

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

Your solution is excellent! It efficiently solves the problem with optimal time and space complexity. The code is clean, readable, and straightforward. You correctly identified that you can capture all profits by buying and selling whenever the next day's price is higher. This greedy approach is perfect for this problem.

One minor improvement: You could add a comment explaining why this greedy approach works (e.g., "We can capture all increasing segments by adding every positive daily difference"). Also, consider using more descriptive variable names, though profit and prices are clear in this context.

Overall, great job! This is the standard solution for this problem.

VERDICT: PASS


Interview Problem: Peeking Iterator (problem2.py)

Your solution has the right idea of storing the next element in a temporary variable for peeking. However, there are some critical issues to address:

  • Avoid using sentinel values like -1: Since the problem allows integers, using -1 to represent "no element" is incorrect because -1 might be a valid element in the iterator. Instead, use None to represent the absence of a next element. This is more Pythonic and avoids conflicts.

  • Correctness in hasNext(): The hasNext() method should not rely on peek() returning -1. Instead, it should check whether tempPeak is not None. Similarly, peek() should not return -1 when there's no element; it should return None or throw an exception. But since the problem states that all calls to peek and next are valid, you can assume that when peek() is called, there is always a next element. Therefore, you don't need to handle the case where tempPeak is None in peek().

  • Initialization: In the constructor, you set tempPeak to the next element if the iterator has next. This is correct. But if the iterator is empty, tempPeak should be None.

  • Updating tempPeak in next(): In the next() method, you should store the current tempPeak (which is the next element to return) in a temporary variable, then update tempPeak to the next element from the iterator (if exists), and then return the stored value. This is what you are doing, but you are using peek() which is unnecessary. Instead, directly use self.tempPeak.

Here is a corrected version of your code:

class PeekingIterator:
    def __init__(self, iterator):
        self.iterator = iterator
        self.tempPeak = self.iterator.next() if self.iterator.hasNext() else None

    def peek(self):
        # Since all calls to peek are valid, we can assume tempPeak is not None
        return self.tempPeak

    def next(self):
        # Store the current tempPeak which is the next value to return
        temp = self.tempPeak
        # Update tempPeak to the next element if exists
        if self.iterator.hasNext():
            self.tempPeak = self.iterator.next()
        else:
            self.tempPeak = None
        return temp

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

This version uses None to indicate no next element and avoids the sentinel value -1. Also, the methods are more efficient and correct.

VERDICT: NEEDS_IMPROVEMENT

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