Problem : https://www.hackerrank.com/challenges/welcome-to-java/problem
Solution : public class Solution {Wednesday, December 30, 2020
Welcome to Java! | HackerRank | Java | Easy | Solution
Tuesday, December 29, 2020
StringStream | HackerRank | Solution | C++ | Easy
Problem : https://www.hackerrank.com/challenges/c-tutorial-stringstream/problem
Solution :
#include <sstream>
Variable Sized Arrays | Hackerrank | Solution | C++
Question : https://www.hackerrank.com/challenges/variable-sized-arrays/problem
Solution : #include <cmath>
/* Free your memory using this code
for(int i = 0; i < size; ++i)
{
delete[] A[i];
}
delete[] A;
*/
Monday, December 28, 2020
Arrays Introduction | Hackerrank | C++ | Easy | Solution
Problem : https://www.hackerrank.com/challenges/arrays-introduction/problem
Solution :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void reverse(int A[],int size){
for(int i = size -1 ;i>=0;i--){
cout<<A[i]<<" ";
}
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int size;
cin>>size;
int A[size];
for(int i =0;i<size;i++){
cin>>A[i];
}
reverse(A,size);
return 0;
}
Pointers : Hackerrank | Easy | Solution | C++
Problem : https://www.hackerrank.com/challenges/c-tutorial-pointer/problem
Solution :
#include <stdio.h>
#include<math.h>
void update(int *pa,int *pb) {
// Complete this function
int temp = *pa;
*pa = *pa + *pb;
*pb = abs(temp - *pb);
}
int main() {
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
Friday, November 27, 2020
Redundant Braces | Amazon | InterviewBit | Solution | C++
Problem :
Given a string A denoting an expression. It contains the following operators ’+’, ‘-‘, ‘*’, ‘/’.
Chech whether A has redundant braces or not.
Return 1 if A has redundant braces, else return 0.
Note: A will be always a valid expression.
Solution:
int Solution::braces(string A) {
// create a stack
stack<char>s;
// process string char by char
int l = A.length();
for(int i =0;i<l;i++){
char c = A[i];
// if c is ( or operator or operand , we push in s
if(c == '(' || (c >= 'a' && c<='z') || c == '+'||c=='-'||c=='*'||c=='/'){
s.push(c);
}
else{
// else
// pop unless we get opening bracket and check the length of popped elements
// if it is > 1
//else end processing and it is redundant
// (a + b) -> 5
int p = 0;
while(s.empty() == false && s.top()!='('){
p++;
s.pop();
}
s.pop();
if(p>1)continue;
else{
return 1;
}
}
}
return 0;
}
Monday, November 23, 2020
Simplify Directory Path | InterviewBit | Microsoft | Solution | C++
Problem :
Given a string A representing an absolute path for a file (Unix-style).
Return the string A after simplifying the absolute path.
Note:
Absolute path always begin with ’/’ ( root directory ).
Path will not have whitespace characters.
Balanced Parantheses! | InterviewBit | Amazon | Google
Problem :
Given a string A consisting only of '(' and ')'.
You need to find whether parantheses in A is balanced or not ,if it is balanced then return 1 else return 0.
Problem Constraints
1 <= |A| <= 105
Input Format
First argument is an string A.
Output Format
Return 1 if parantheses in string are balanced else return 0.
Solution :
int Solution::solve(string A) {
int l = A.length();
stack<char>s;
int f = 0;
for(int i =0;i<l;i++){
char c = A[i];
if(c == '('){
s.push(c);
}else{
if(s.empty()){
f = 1;
break;
}
s.pop();
}
}
if(f == 1){
return 0;
}
if(!s.empty()){
return 0;
}
return 1;
}
Sunday, November 22, 2020
Reverse String | interviewBit | Solution | C++
Problem :
Given a string S, reverse the string using stack.
Example :
Input : "abc"
Return "cba"
Wednesday, November 4, 2020
Queue Using Stack | Code | C++
#include<iostream>
#include<stack>
using namespace std;
stack<int>s1;
stack<int>s2;
void push(int element){
s1.push(element);
}
int pop(){
if(!s2.empty()){
int r = s2.top();
s2.pop();
return r;
}else{
while(!s1.empty()){
int t = s1.top();
s1.pop();
s2.push(t);
}
int r = s2.top();
s2.pop();
return r;
}
}
int main(){
// s1 and s2
//push : 1 2 3 4
//pop : -> 1
//push : 5 -> 2 3 4 5
//pop : -> 2
push(1);
push(2);
push(3);
push(4);
cout<<pop();
push(5);
cout<<'\n'<<pop();
return 0;}
NOTE : Write code for queue properties and even do check for empty before popping the element from the queue ...
Tuesday, November 3, 2020
Stack Using STL : CODE | C++
// stack using STL
#include<stack>
#include<iostream>
using namespace std;
int main(){
stack<int>s;
// push
s.push(2);
s.push(4);
s.push(5);
s.push(7);
// 2 4 5 7
//pop
s.pop(); // removed 7
//top
cout<<" top = "<<s.top(); // 5
// is empty
if(s.empty())cout<<"\nyes";
else cout<<"\nNO";
// size
cout<<" \nsize = "<<s.size(); // 3
}
Stack Using Linked List : Code ( C++ )
CODE:
// header file
#include<stdio.h>
#include<stdlib.h>
// struct
struct node{
int data;
struct node* next;
};
// operations
//push
void push(struct node ** head , int element){
struct node* p = (struct node * )malloc(sizeof(struct node));
p->data = element;
//**head -> *head(top of the stack) -> stack
p->next = *head;
*head = p;
}
// isEmpty
int isEmpty(struct node *top){
return top == NULL;
}
// pop
int pop(struct node ** top ){
if(isEmpty(*top)){
return -1;
}else{
//**head -> *head(top of the stack) -> stack [1 , 2 ,3 ,...]
//*p = 1
//*head = *head->next
//free(p)
struct node* p;
p = *top;
*top = (*top)->next;
int r = p->data;
free(p);
return r;
}
}
// top
int peek(struct node* top){
if(!isEmpty(top)){
return top->data;
}else{
return -1;
}
}
// main
int main(){
//**q = &*p = &a
struct node* top = NULL;
push(&top,1);
push(&top,2);
push(&top,3);
// 3 2 1
printf("%d",peek(top));
return 0;}
Sunday, November 1, 2020
Code of Stack in C Using Array
// stack using array
// library
#include<stdio.h>
#include<stdlib.h>
// make structure maxsize , top , *items
struct stack{
int maxSize;
int top;
int *items;
};
// initialization
struct stack* newStack(int capacity){
struct stack *p = (struct stack *)malloc(sizeof(struct stack));
p->maxSize = capacity;
p->top = -1;
p->items = (int *)malloc(sizeof(int) * capacity);
return p;
}
// operations for our stack
// top
int top(struct stack* p ){
return p->items[p->top];
}
// isEmpty
int isEmpty(struct stack* p ){
return p->top == -1;
}
// isFull
int isFull(struct stack *p){
return p->top + 1 == p->maxSize;
}
// size
int size(struct stack * p){
return p->top + 1;
}
// push
void push(struct stack* p , int element){ // 12 3 4 4 3
if(isFull(p)){
printf("Overflow!\n");
}else{
p->items[++p->top] = element;
}
}
// pop
int pop(struct stack * p ){
if(isEmpty(p)){
printf("Underflow!");
}else{
return p->items[p->top--];
}
}
int main(){
struct stack * s = newStack(5);
push(s,1);
push(s,5);
push(s,3);
push(s,6);
// 1 5 3 6
printf("%d\n",top(s));
printf("%d",size(s));
return 0;
}