Update appNew update is available. Click here to update.
About
I'm a prefinal year student in Ahmedabad, specializing in full-stack MERN development. I have a keen interest in competitive programming (CP) and I'm actively learning machine learning (ML).
Adani Institute Of Infrastructure Management 2025
C++ - Default language
My Stats
EXP gained
yellow-spark
12082
Level
7 (Expert)
Community stats
Discussions
3
Upvotes
1
Know more
Weekend contest rating
Contest attended
Problems solved
2021 2023
Better than %
Weekend contest rating
Contest attended
Problems solved
2021 2023
Better than %
173
Total problems solved
115
Easy
50
Moderate
8
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:

14 days

Less

More

Achievements
6
Ronin
Topics
Binary Search
Recursion
+ 4 more
2
Samurai
Topics
Arrays
Binary Trees
Discussions
simple cpp soln find path of each then compare paths
Interview problems

bool findpath(vector<int>&v,TreeNode<int>*root,int x){

    if(root==NULL){

    

        return false;

    }

    if(root->data==x){

        v.push_back(root->data);

        return true;

    }else{

        v.push_back(root->data);

    }

    bool left = findpath(v,root->left,x);

    bool right =findpath(v,root->right,x);

    if(left==0 && right==0){

        v.pop_back();

        return false;

    }else{

        return true;

    }

}

int lowestCommonAncestor(TreeNode<int> *root, int x, int y)

{

    //    Write your code here

    vector<int>pathx;

    vector<int>pathy;

    findpath(pathx,root,x);

    findpath(pathy,root,y);

    int ans=0;

    int i=0;

    int j=0;

    while(i<pathx.size() && j<pathy.size()){

        if(pathx[i]==pathy[j]){

            ans=pathx[i];

            i++;

            j++;

 

        }else{

            break;

        }

    }

    return ans;

 

}

profile
afkcoder
Published On 23-Nov-2023
32 views
1 replies
1 upvotes
easy cpp soln
Interview problems

 

bool solve(vector<int>&v,TreeNode<int>*root,int x){

   //if root is null means using this path we didnt get our element hence return false

 if(root==NULL){

    

        return false;

    }

//if we got the element return true no need to traverse //more

    if(root->data==x){

        v.push_back(root->data);

        return true;

    }

//else still in search add the curr node 

else{

        v.push_back(root->data);

    }

// recursively traverse in both the directions

    bool left = solve(v,root->left,x);

    bool right = solve(v,root->right,x);

//if after traversing in both the direction of the //respective node you didnt get x then 

// we remove respective node as taking this node we //cant get to out x

    if(left==0 && right==0){

        v.pop_back();

        return false;

    }

// if in any one direction we get true means taking this node we can get to our element hence return true

else{

        return true;

    }

}

vector<int> pathInATree(TreeNode<int> *root, int x)

{

    // Write your code here.

    vector<int>v;

    solve(v,root,x);

    return v;

}

profile
afkcoder
Published On 23-Nov-2023
6 views
0 replies
0 upvotes
easy c++ soln .In sample testcases 24 th testcase is not passing please someone can find the error
Interview problems

struct point{

    int value;

    int x;

    int y;

};

void travel(TreeNode<int>*root,vector<point>&a,int x,int y){

    if(root==NULL){

        return;

    }

    point store;

    store.value = root->data;

    store.x= x;

    store.y=y;

    a.push_back(store);

    travel(root->left,a,x-1,y+1);

    travel(root->right,a,x+1,y+1);

 

}

bool compare(point a,point b){

    if(a.x!=b.x){

        return a.x<b.x;

    }else{

        return a.y<b.y;

    }

}

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

{

    vector<point>a;

    vector<int>v;

    travel(root,a,0,0);

    // Write your code here.

    sort(a.begin(),a.end(),compare);

    int lastaken = a[0].x;

    v.push_back(a[0].value);

    for(int i=0;i<a.size();i++){

        if(a[i].x>lastaken){

            v.push_back(a[i].value);

            lastaken = a[i].x;

        }

    }

    return v;

    

 

}

profile
afkcoder
Published On 22-Nov-2023
16 views
0 replies
0 upvotes
easiest solution using custom data structure
Interview problems

//creating custom data type for ease in manipulation //of coordinates as per problem

struct point{

    int value;

    int x;

    int y;

};

//here we iterate through the tree and mark //coordinates ans store coordinates and values in our //vector

void travel(TreeNode<int>*root,int x,int y,vector<point>&save){

    if(root==NULL){

        return;

    }

    point a ;

    a.value=root->data;

    a.x =x;

    a.y=y;

    save.push_back(a);

 

    travel(root->left,x-1,y+1,save);

    travel(root->right,x+1,y+1,save);

 

}

// main sorting function as per our problem requirement

bool compare(point a,point b){

    if(a.x!=b.x){

        return a.x<b.x ;

    }else if(a.x==b.x){

        if(a.y!=b.y){

            return a.y<b.y;

        }

        return a.value<b.value;

        

    }

}

// combining and using all logic and utility func

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

{

    // Write your code here.

//create a custom datastructure vector

    vector<point>save;

    vector<int>ans;

//get coordinates ans fill the vector

    travel(root,0,0,save);

//sort as per requirement

    sort(save.begin(),save.end(),compare);

//lastly store and return ans in vector<int>form

    for(auto i:save){

        ans.push_back(i.value);

    }

    return ans;

 

}

profile
afkcoder
Published On 22-Nov-2023
9 views
0 replies
0 upvotes
simple cpp soln whats the issue
Interview problems

The idea is to have enough balance in the account to give exchange for every iteration if not return false

bool lemonadeChange(vector<int> &bill) {

    // Write your code here.

    

    long long acc=0;

    for(int i=0;i<bill.size();i++){

        

        if(bill[i]==5){

            acc+=5;

        }else if(bill[i]==10){

            

            if(acc>=5){

                acc+=5;

            }else{  

                return false;

            }

        }else if(bill[i]==20){

            if(acc>=15){

                acc+=5;

            }else{

                return false;

            }

        }

    }

        return true;

}

profile
afkcoder
Published On 17-Nov-2023
15 views
0 replies
0 upvotes
simple max of both cases soln .fails at testcase 6. Can you spot the error
Interview problems

Can you spot the mistake?

#include <bits/stdc++.h> 

long long int houseRobber(vector<int>& valueInHouse)

{

    // Write your code here.

    if(valueInHouse.size()==1){

        return valueInHouse[0];

    }

    long long int prev=0;

    long long int prev2=0;

    for(int i=1;i<valueInHouse.size();i++){

        int pick = valueInHouse[i]+prev2;

        int notpick = 0 + prev;

        prev2= prev;

        prev = max(pick,notpick);

    }

    long long int prev3=valueInHouse[0];

    long long int prev4=0;

    for(int i=1;i<valueInHouse.size()-1;i++){

 

        int pick=valueInHouse[i]+prev4;

        int notpick = 0+prev3;

        prev4= prev3;

        prev3=max(pick,notpick);

    }

    return max(prev,prev3);

 

}

profile
afkcoder
Published On 15-Nov-2023
35 views
0 replies
0 upvotes
is this logic correct?
Interview problems

#include<bits/stdc++.h>

    template <typename T>

    

    class BinaryTreeNode{

    public:

        T data;

        BinaryTreeNode<T> *left;

        BinaryTreeNode<T> *right;

 

        BinaryTreeNode(T data) {

            this -> data = data;

            left = NULL;

            right = NULL;

        }

    };

 

void travelll(BinaryTreeNode<int>* root,string str){

    if(root==NULL){

        return;

    }

    str+=to_string(root->data);

    travelll(root->left,str);

    travelll(root->right,str);

}

string printLargest(BinaryTreeNode<int>* root) {

    // Write your code here.

    string str ;

    travelll(root,str);

    int arr[10]={0};

    for(i:str){

        arr[i-'0']++;

    }

    string ans= "";

    for(int i=9;i>=0;i--){

        if(arr[i]!=0){

            while(arr[i]--){

                ans+=to_string(i);

               

            }

        }

    }

    return ans;

}

 

profile
afkcoder
Published On 29-Oct-2023
36 views
0 replies
0 upvotes
Easy c++ solution runtime error, can you spot the error
Interview problems

int maxind(vector<vector<int>> &arr,int n,int m,int col){

    int maxi=-1;

    int index =-1;

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

        if(arr[i][col]>maxi){

            maxi = arr[i][col];

            index = i;

        }

    }

    return index;

}

vector<int> findPeakGrid(vector<vector<int>> &arr){

    // Write your code here.

    int n = arr.size();

    int  m = arr[0].size();  

    int low = 0;

    int high=m-1;

    while(low<=high){

        int mid = low+(high-low)/2;

        int maxindex = maxind(arr,n,m,mid);

        int left = mid-1>=0?arr[maxindex-1][mid]:-1;

        int right = mid+1<=m-1?arr[maxindex+1][mid]:-1;

        if(left<arr[maxindex][mid] && right<arr[maxindex][mid]){

            return {maxindex,mid};

        }else if(left>arr[maxindex][mid]){

            low = mid-1;

        }else{

            high = mid+1;

        }

        

    }

    return {-1,-1};

      

}

profile
afkcoder
Published On 12-Oct-2023
26 views
1 replies
0 upvotes
Easy c++ solution runtime error, can you spot the error
Interview problems

int maxind(vector<vector<int>> &arr,int n,int m,int col){

    int maxi=-1;

    int index =-1;

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

        if(arr[i][col]>maxi){

            maxi = arr[i][col];

            index = i;

        }

    }

    return index;

}

vector<int> findPeakGrid(vector<vector<int>> &arr){

    // Write your code here.

    int n = arr.size();

    int  m = arr[0].size();  

    int low = 0;

    int high=m-1;

    while(low<=high){

        int mid = low+(high-low)/2;

        int maxindex = maxind(arr,n,m,mid);

        int left = mid-1>=0?arr[maxindex-1][mid]:-1;

        int right = mid+1<=m-1?arr[maxindex+1][mid]:-1;

        if(left<arr[maxindex][mid] && right<arr[maxindex][mid]){

            return {maxindex,mid};

        }else if(left>arr[maxindex][mid]){

            low = mid-1;

        }else{

            high = mid+1;

        }

        

    }

    return {-1,-1};

      

}

profile
afkcoder
Published On 12-Oct-2023
26 views
1 replies
0 upvotes
Easy c++ solution runtime error, can you spot the error
Interview problems

int maxind(vector<vector<int>> &arr,int n,int m,int col){

    int maxi=-1;

    int index =-1;

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

        if(arr[i][col]>maxi){

            maxi = arr[i][col];

            index = i;

        }

    }

    return index;

}

vector<int> findPeakGrid(vector<vector<int>> &arr){

    // Write your code here.

    int n = arr.size();

    int  m = arr[0].size();  

    int low = 0;

    int high=m-1;

    while(low<=high){

        int mid = low+(high-low)/2;

        int maxindex = maxind(arr,n,m,mid);

        int left = mid-1>=0?arr[maxindex-1][mid]:-1;

        int right = mid+1<=m-1?arr[maxindex+1][mid]:-1;

        if(left<arr[maxindex][mid] && right<arr[maxindex][mid]){

            return {maxindex,mid};

        }else if(left>arr[maxindex][mid]){

            low = mid-1;

        }else{

            high = mid+1;

        }

        

    }

    return {-1,-1};

      

}

profile
afkcoder
Published On 12-Oct-2023
26 views
1 replies
0 upvotes