This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Find the row with maximum number of 1s | |
Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s. | |
Example | |
Input matrix | |
0 1 1 1 | |
0 0 1 1 | |
1 1 1 1 // this row has maximum 1s | |
0 0 0 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Check if a number is Palindrome | |
*/ | |
#include <iostream> | |
#include<cstring> | |
using namespace std; | |
#include<cmath> | |
bool isPalindrome(int n) | |
{ | |
int ndigit=0; | |
int num=n; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Input arr[] = [1, 2, 3, 4, 5, 6, 7], | |
d = 2, n =7 | |
Ans:arr[] = [3, 4, 5, 6, 7, 1, 2] | |
solution: (Ar Br)r | |
steps: | |
first reverse d elements | |
then reverse remaining (n-d) elements | |
now reverse entire elements. | |
Example: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HOW TO ROTATE A MATRIX 90 DEGREES WITHOUT USING ANY EXTRA SPACE? | |
Ans: | |
O(n^2) time and O(1) space algorithm ( without any workarounds and hanky-panky stuff! ) | |
Rotate by +90: | |
Transpose | |
Reverse each row |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* strtok example */ | |
#include <stdio.h> | |
#include <string.h> | |
int main () | |
{ | |
char str[] ="- This, a sample string."; | |
char * pch; | |
printf ("Splitting string \"%s\" into tokens:\n",str); | |
pch = strtok (str," ,.-"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mutex: | |
a mutex is locking mechanism used to synchronize access to a resource. Only one task (can be a thread or process based on OS abstraction) can acquire the mutex. It means there will be ownership associated with mutex, and only the owner can release the lock (mutex). | |
Is a key to a toilet. One person can have the key - occupy the toilet - at the time. When finished, the person gives (frees) the key to the next person in the queue | |
Semaphore: | |
Semaphores are typically used as a signaling mechanism between processes. | |
A typical example is a producer-consumer process pair (not explained here to keep the answer to a readable length). | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
class widget | |
{ | |
private: | |
Bitmap *pb1;//pointer to heap allocated object | |
Bitmap *pb2; | |
}; | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Item 12 from effective c++ | |
//helper function to make a log entry | |
void logCall(const std::string & name);//make a log entry | |
//base class | |
class Customer | |
{ | |
public: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(1)DECLTYPE: | |
In the C++ programming language, decltype is a keyword used to query the type of an expression | |
int i = 33; | |
decltype(i) j = i*2; | |
OR | |
decltype(f) f2 = f; // the type of a lambda function is unique and unnamed | |
(2)lexical_cast in boost | |
we can implement like this to server the purpose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given an array A[] consisting 0s, 1s and 2s, write a function that sorts A[]. The functions should put all 0s first, then all 1s and all 2s in last. | |
Example | |
Input = {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}; | |
Output = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2} | |
soln:Dutch National Flag Algorithm, or 3-way Partitioning — | |
*/ | |
void sort012(int a[], int arr_size) |
OlderNewer