Update appNew update is available. Click here to update.
About
Sir Padampat Singhania University 2026
My Stats
EXP gained
yellow-spark
28830
Level
7 (Expert)
Community stats
Discussions
0
Upvotes
0
Know more
517
Total problems solved
452
Easy
59
Moderate
6
Hard
0
Ninja
Jan Jan Feb Feb Mar Mar Apr Apr May May Jun Jun Jul Jul Aug Aug Sep Sep Oct Oct Nov Nov Dec Dec

Current streak:

0 days

Longest streak:

13 days

Less

More

Achievements
3
Ronin
Topics
Sorting
Strings
+ 1 more
4
Samurai
Topics
Stacks & Queues
Arrays
+ 2 more
1
Sensei
Guided path
Basics of C++
Discussions
c++ solution with detail comments
Interview problems

#include<bits/stdc++.h>

int minTimeToRot(vector<vector<int>>& grid, int n, int m)

{

    // making pair for <<i,j>time>

    queue<pair<pair<int,int>,int>> q;

 

    //visited for checking already rotten or not

    int vis[n][m];

 

    //counting fresh oranges and pushing rotting oranges positon with 0 time in queue.

    //also marking visited rotten oranges

    int cntFresh=0;

    for(int i=0; i<n; i++) {

        for(int j=0; j<m; j++) {

            if(grid[i][j]==2) {

                q.push({{i,j},0});

                vis[i][j] = 2;

            }

            else {

                vis[i][j]=0;

            }

            if(grid[i][j]==1) 

            cntFresh++;

        }

    }

 

    int tm = 0;//max time

    int drow[] = {-1,0,1,0};//for update row:  up   same down same

    int dcol[] = {0,1,0,-1};//for update col: same  next same previous

   

    int cnt=0;//counting that all fresh oranges now rotted or not 

   

    while(!q.empty()) {

        int r = q.front().first.first;//taking rotted row

        int c = q.front().first.second;//taking rotted col

        int t = q.front().second;//time

        tm = max(tm,t);//max time, updating every time

        q.pop();

 

        //checking all neighbours for every rotted oranges.

        //updating time with 1

        //and pushing in queue which we are rotting, makring visited

        //counting no of rotted oranges that converted from fresh 

        for(int i=0; i<4; i++) {

            int nrow = r + drow[i];

            int ncol = c + dcol[i];

            if(nrow>=0 && nrow<n && ncol>=0 && ncol<m 

            && vis[nrow][ncol]!=2 && grid[nrow][ncol]!=0 && grid[nrow][ncol]==1) {

                q.push({{nrow, ncol}, t+1});

                vis[nrow][ncol]=2;

                cnt++;//counting no of rotted oranges that converted from fresh 

            }

        }

    }

    // at last if all fresh oranges converted into rotten then cnt==cntfresh return tm else -1 

    if(cnt != cntFresh) return -1;

    return tm;

}

profile
Kushagra Sharma
Published On 18-Nov-2023
34 views
0 replies
0 upvotes
c++ solution using level order approch.
Interview problems

#include<bits/stdc++.h>

using namespace std;

vector<int> printLeftView(BinaryTreeNode<int> *root)

{

    

    queue<BinaryTreeNode<int>*>q;

 

    q.push(root);

    

    vector<int>v;

    

    while(!q.empty()){

        int n=q.size();

        for(int i=0;i<n;i++){

 

        BinaryTreeNode<int>*cur=q.front();

        q.pop();

        

        

        if(i==0)

        v.push_back(cur->data);

 

        if(cur->left){

            q.push(cur->left);

        }

        if (cur->right) {

            q.push(cur->right);

        }

        }

    }

    return v;

 

    }

 

 

profile
Kushagra Sharma
Published On 28-Oct-2023
106 views
0 replies
0 upvotes
C++ solution using level order traversal approch.
Interview problems

vector<int> getLeftView(TreeNode<int> *root)

{

    //    Write your code here

    queue<TreeNode<int>*>q;

    vector<int>v;

    if(root==NULL)

    return v;

    q.push(root);//q ki size 1 hai.

    

    while(!q.empty()){

        

        int n=q.size();//har level ka size n me store krte hai har bar.

        

        for(int i=0;i<n;i++){

         TreeNode<int>*cur=q.front();

         q.pop();

         

         if(i==0){//jb nye level pr jate hai to uska 1st element left view hoga.

             v.push_back(cur->data);

         }

         

         if(cur->left){

             q.push(cur->left);

         }

         

         if(cur->right)

         q.push(cur->right);

        }

    }

    return v;

}

profile
Kushagra Sharma
Published On 28-Oct-2023
65 views
0 replies
1 upvotes
C++ solution using level order traversal approch.
Interview problems

vector<int> getLeftView(TreeNode<int> *root)

{

    //    Write your code here

    queue<TreeNode<int>*>q;

    vector<int>v;

    if(root==NULL)

    return v;

    q.push(root);//q ki size 1 hai.

    

    while(!q.empty()){

        

        int n=q.size();//har level ka size n me store krte hai har bar.

        

        for(int i=0;i<n;i++){

         TreeNode<int>*cur=q.front();

         q.pop();

         

         if(i==0){//jb nye level pr jate hai to uska 1st element left view hoga.

             v.push_back(cur->data);

         }

         

         if(cur->left){

             q.push(cur->left);

         }

         

         if(cur->right)

         q.push(cur->right);

        }

    }

    return v;

}

profile
Kushagra Sharma
Published On 28-Oct-2023
65 views
0 replies
1 upvotes
c++ solution simple.
Interview problems

Node* sortList(Node *head){

 

    // Write your code here.

    Node * temp=head;

    stack<int>st;

    while(temp){

        if(temp->data==2)

         st.push(temp->data);

         temp=temp->next;

    }

    temp=head;

    while(temp){

        if(temp->data==1)

         st.push(temp->data);

         temp=temp->next;

    }

    temp=head;

    while(temp){

        if(temp->data==0)

         st.push(temp->data);

         temp=temp->next;

    }

    temp=head;

    while(!st.empty()){

        temp->data=st.top();

        st.pop();

        temp=temp->next;

    }

    

    return head;

}

 

profile
Kushagra Sharma
Published On 21-Oct-2023
152 views
0 replies
0 upvotes
Solution using two stacks... (72/80) time complexity issue...
Interview problems

#include<stack>

using namespace std;

class Browser

{

    string cur;

    stack<string>st1;

    stack<string>st2;

    public:

    

    Browser(string &homepage)

    {   

        cur =homepage;

        // Write you code here

    }

 

    void visit(string &url)

    { 

        st1.push(cur);

        cur=url;

 

        while(!st2.empty()){

            st2.pop();

        }

        // stack<string>temp;

        // st2=temp;

        // Write you code here

    }

 

    string back(int steps)

    {

 

     while(steps>0 && st1.empty()==false){

         st2.push(cur);

         cur=st1.top();

         st1.pop();

         steps--;

 

     }

     return cur;

        // Write you code here

    }

 

    string forward(int steps)

    {

        while(steps>0 && st2.empty()==false){

            st1.push(cur);

            cur=st2.top();

            st2.pop();

            steps--;

        }

        return cur;

        // Write you code here

    }

};

profile
Kushagra Sharma
Published On 15-Oct-2023
54 views
0 replies
0 upvotes
c++ solution using Two stacks
Interview problems

#include<bits/stdc++.h>

using namespace std;

 

class Queue {

    // Define the data members(if any) here.

    stack<int>s1,s2;

 

    public:

    Queue() {

        // Initialize your data structure here.

    }

 

    void enQueue(int val) {

        s1.push(val);

        // Implement the enqueue() function.

    }

 

    int deQueue() {

        if(s1.empty()){

            return -1;

        }

        else{

            while(!s1.empty()){

                s2.push(s1.top());

                s1.pop();

            }

           int x= s2.top();

           s2.pop();

           while(!s2.empty()){

                s1.push(s2.top());

                s2.pop();

            }

            return x;

        }

 

        // Implement the dequeue() function.

    }

 

    int peek() {

        if(s1.empty()){

            return -1;

        }

        else{

            while(!s1.empty()){

                s2.push(s1.top());

                s1.pop();

            }

           int x= s2.top();

           while(!s2.empty()){

                s1.push(s2.top());

                s2.pop();

            }

           return x;

           }

        // Implement the peek() function here.

    }

 

    bool isEmpty() {

        if(s1.empty()){

            return true;

 

        }

        else{

            return false;

        }

        // Implement the isEmpty() function here.

    }

};

profile
Kushagra Sharma
Published On 15-Oct-2023
312 views
0 replies
0 upvotes
brute force(100%) and recursion both.
Interview problems

#include<bits/stdc++.h>

using namespace std;

 

void helpSort(stack<int>&s,int num){

 

    if(s.empty() || s.top()>num ){

        s.push(num);

        return; 

    }

    

        

    int item=s.top();

    s.pop();

    helpSort(s, num) ;

    s.push(item);

        

    

}

 

void sortStack(stack<int>&s){

    if(s.empty()){

 

        return;

    }

    int num=s.top();

    s.pop();

    sortStack(s);

    helpSort(s,num);

 

}

 

 

// void sortStack(stack<int> &stack)

// {

//  // Write your code here

//  vector<int>v(stack.size());

//  int i=0;

//  while(!stack.empty()){

//      v[i]=stack.top();

//      stack.pop();

//      i++;

//  }

//  sort(v.begin(),v.end());

//  for(int i=v.size()-1;i>=0;i--){

//      stack.push(v[i]);

//  }

    

// }

profile
Kushagra Sharma
Published On 06-Oct-2023
51 views
0 replies
0 upvotes
c++ solution using stack.
Interview problems

#include<bits/stdc++.h>

string reverseString(string &str) {

   

    stack<string> st;

 

    for (int i = 0; i < str.length();) {

        // Skip leading spaces

        while (i < str.length() && str[i] == ' ') {

            i++;

        }

 

        // Extract the word

        string c = "";

        while (i < str.length() && str[i] != ' ') {

            c += str[i];

            i++;

        }

 

        if (!c.empty()) {

            st.push(c);

        }

    }

 

    string s;

    while (!st.empty()) {

        s += st.top();

        s += " ";

        st.pop();

    }

 

    // Remove the trailing space

    if (!s.empty()) {

        s.pop_back();

    }

 

    return s;

}

 

profile
Kushagra Sharma
Published On 01-Sep-2023
172 views
0 replies
0 upvotes