Skip to content

Complete Competitive-Coding-10 - #474

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

Complete Competitive-Coding-10#474
PrakarshKamal wants to merge 1 commit into
super30admin:masterfrom
PrakarshKamal:master

Conversation

@PrakarshKamal

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Interview Problem : Buy and Sell Stock (BestTimeBuySellStockII.java)

Your solution is excellent! You have implemented an efficient greedy algorithm that correctly solves the problem. The code is clean and easy to understand. Here are some strengths:

  • You used a greedy approach which is the best way to solve this problem.
  • The time complexity is O(n) and space complexity is O(1), which is optimal.
  • The code is concise and well-structured.

One minor suggestion: While the code is already good, you might consider adding a comment explaining why the greedy approach works (i.e., because we can capture every positive price movement without any restriction on the number of transactions). This would help others understand the intuition behind the solution.

Overall, great job!

VERDICT: PASS


Interview Problem: Peeking Iterator (PeekingIterator.java)

Your solution is excellent! You correctly implemented the PeekingIterator with O(1) time and space complexity for all operations. You also provided a brute force solution initially, which shows you considered alternative approaches, but then you implemented the optimal solution.

Strengths:

  • You used a single variable nextVal to cache the next element, which is efficient.
  • Your code is clean, well-commented, and easy to read.
  • You handled the case when the iterator is empty by setting nextVal to null.

Areas for improvement:

  • In the constructor, you can set nextVal more concisely by using the ternary operator or simply calling itr.next() if itr.hasNext() is true, but your current approach is clear.

  • In the next() method, you can write it as:

    Integer temp = nextVal;
    nextVal = itr.hasNext() ? itr.next() : null;
    return temp;
    

    This reduces the number of lines slightly but maintains clarity.

  • Note that the problem states that all calls to next and peek are valid, so you don't need to worry about calling next() when there are no elements, but your implementation correctly handles it.

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

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