Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added some top most interview problems #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions Famous-Coding-Interview-Problems/MergeKSortedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

// 4. link : [ https://leetcode.com/problems/merge-k-sorted-lists ]


/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:


ListNode* merge(ListNode* l1, ListNode* l2) {
if(l1 == NULL && l2 == NULL)
return NULL;
if(l1==NULL)
return l2;
if(l2==NULL)
return l1;

ListNode* temp1 = l1;
ListNode* temp2 = l2;

ListNode* tail = new ListNode(0);
ListNode* head = tail;


while(temp1 && temp2){
if(temp1->val <= temp2->val){
ListNode* newNode = new ListNode(temp1->val);
tail->next = newNode;
tail = newNode;
temp1 = temp1->next;
}else{
ListNode* newNode = new ListNode(temp2->val);
tail->next = newNode;
tail = newNode;
temp2 = temp2->next;
}
}

while(temp1){
ListNode* newNode = new ListNode(temp1->val);
tail->next = newNode;
tail = newNode;
temp1 = temp1->next;
}

while(temp2){
ListNode* newNode = new ListNode(temp2->val);
tail->next = newNode;
tail = newNode;
temp2 = temp2->next;
}

return (head->next);
}

ListNode* helper(vector<ListNode*>& lists, int l, int r){
if(l == r)
return lists[l];

int mid = l+(r-l)/2;

ListNode* h1 = helper(lists,l,mid);
ListNode* h2 = helper(lists,mid+1,r);

return merge(h1,h2);

}

ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.size() == 0)
return NULL;

int l = 0;
int r = lists.size()-1;

return helper(lists,l,r);
}
};
77 changes: 77 additions & 0 deletions Famous-Coding-Interview-Problems/firstAndLastPos.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

// 7 . link : [ https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array ]

#include<bits/stdc++.h>
using namespace std;

int binarySearch(vector<int>& nums, int l,int r, int target){
if(l>r){
return -1;
}
int mid = l+(r-l)/2;

if(nums[mid] == target)
return mid;
else if(nums[mid] > target)
return binarySearch(nums,l,mid-1,target);
else
return binarySearch(nums,mid+1,r,target);
}

vector<int> searchRange(vector<int>& nums, int target) {
int pos = binarySearch(nums,0,nums.size()-1, target);

if(pos < 0)
return {-1,-1};
int left = pos;
int right = pos;

while(left > 0){
if(nums[left-1] != target)
break;
left--;
}
while(right < nums.size()-1){
if(nums[right+1] != target)
break;
right++;
}

return {left,right};
}

/*
1
9
5 7 7 8 8 8 9 10 11
8


3 5
*/

int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif

int test;
cin >> test;

while(test--){
int n;
cin >> n;
vector<int> arr(n);
for(int i=0; i<n; i++)
cin >> arr[i];

int target ; cin >> target;

vector<int> index = searchRange(arr , target);
cout << index[0] << " " << index[1] << endl;;
}


return 0;
}
45 changes: 45 additions & 0 deletions Famous-Coding-Interview-Problems/longestWord.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <bits/stdc++.h>
using namespace std;

string LongestWord(string sen) {

// code goes here
string ans="";
string currStr="";


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

if(sen[i] >= 'a' && sen[i] <= 'z'){
currStr += sen[i];
}else{
if(currStr.size() > ans.size()){
ans = currStr;
currStr = "";
}
}
}

if(currStr.size() > ans.size())
ans = currStr;

return ans;

}



int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif

cout << LongestWord("fun&!! time");

string s = "fun&!! time";


return 0;

}
123 changes: 123 additions & 0 deletions Famous-Coding-Interview-Problems/mergeTwoSortedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

// 3. link : [ https://leetcode.com/problems/merge-two-sorted-lists ]

#include<bits/stdc++.h>
using namespace std;

// node class
class ListNode{
public:
int val;
ListNode* next;

ListNode(int data){
this->val = data;
next = NULL;
}

};


ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* temp1 = l1;
ListNode* temp2 = l2;

ListNode* tail = new ListNode(0);
ListNode* head = tail;


while(temp1 && temp2){
if(temp1->val <= temp2->val){
ListNode* newNode = new ListNode(temp1->val);
tail->next = newNode;
tail = newNode;
temp1 = temp1->next;
}else{
ListNode* newNode = new ListNode(temp2->val);
tail->next = newNode;
tail = newNode;
temp2 = temp2->next;
}
}

while(temp1){
ListNode* newNode = new ListNode(temp1->val);
tail->next = newNode;
tail = newNode;
temp1 = temp1->next;
}

while(temp2){
ListNode* newNode = new ListNode(temp2->val);
tail->next = newNode;
tail = newNode;
temp2 = temp2->next;
}

return head->next;
}

ListNode* takeInput(){
ListNode* head = NULL;
ListNode* tail = NULL;

int data;
while(cin >> data && data != -1){
ListNode* node = new ListNode(data);
if(head == NULL){
head = node;
tail = node;
}else{
tail->next = node;
tail = node;
}
}
return head;
}

void print(ListNode* head){
if(head == NULL)
return;

while(head){
cout << head->val << " ";
head = head->next;
}
cout << endl;
}



/*
1
1
1 3 4 5 6 -1
-2 -4 3 7 9 -1

-2 -4 1 3 3 4 5 6 7 9

*/

int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif

int test;
cin >> test;

while(test--){

ListNode* l1 = takeInput();
ListNode* l2 = takeInput();

ListNode* head = mergeTwoLists(l1, l2);
print(head);


}


return 0;
}
Loading