const arr = ["Even", "Odd"] //The positions of even and odd can't be changes as even=0 and odd=1
cin >> n;
cout << arr[n % 2]
https://github.com/Hasandev08/Interview-Questions/blob/master/Programs/Queue%20from%20stack.cpp
void pairWiseSwap(Node* head)
{
Node* temp = head;
while(temp != NULL && temp->next != NULL)
{
swap(temp, temp->next)
temp = temp->next->next
}
}
void reverse ()
{
Node* current = head; //This node is initally on the head of the linked list
Node* next = NULL; //These are the nodes initially pointing to NULL
Node* prev = NULL;
while(current != NULL) //traversing the linked list
{
next = current->next; //the temp will go to the node that is next to the current node
current->next = prev; //the current node that was pointing on the next node will start pointing the previous node
prev = current; //the previous node pointer will be moved one step so that it start pointing the node on which current is pointing
current = next; //the current will also be moved one step further so that the next nodes can be reversed
}
head = prev
}
void deleteAtHead()
{
Node* temp = head;
head = head->next;
delete(temp);
temp = NULL;
}
void deleteAtMid()
{
Node* current = head;
Node* prev = NULL;
Node* next = NULL;
for(int i = 0; i < position; i++)
{
prev = current;
current = current->next;
next = current->next;
}
prev->next = current->next;
free(current);
current = NULL;
}
void deleteAtEnd()
{
Node* temp = head;
while(temp != NULL)
{
temp = temp->next;
}
free(temp);
temp = NULL;
}
function moveAtEnd(arr, n) {
let count = 0;
for(let i = 0; i < n; i++)
if(arr[i] !== 0)
arr[count++] = arr[i]
while(count < n) arr[count++] = 0
}
https://github.com/Hasandev08/Interview-Questions/blob/master/Programs/Middle%20Node.cpp
void lastNode(Node* head)
{
Node* temp = head;
while(temp != NULL)
{
temp = temp->next; //traversing to the last node
}
cout << temp->data; //printing the last node of the linked list
}
int reverseArray(int arr[], int start, int end)
{
while(start < end)
{
int temp = arr[start]
arr[start] = arr[end]
arr[end] = temp
start++
end--
}
}
https://github.com/Hasandev08/Interview-Questions/blob/master/Programs/Deleting%20Middle%20Node.cpp
- Libraries provide developers with predefined functions and classes to make their work easier and boost the development process. Framework, on the other hand, is like the foundation upon which developers build applications for specific platforms
- Singleton is like a single resource which is being shared among multiple users; for example sharing a single washing machine among all the residents in a hotel or sharing a single appliance like refrigerator among all the family members.
https://github.com/Hasandev08/Interview-Questions/blob/master/Programs/Right%20Sum.cpp
https://github.com/Hasandev08/Interview-Questions/blob/master/Programs/Swap%20without%20third.cpp
int removeDuplicate(int arr[], int n)
{
int temp[n]
int j = 0
for (int i = 0; i < n-1; i++)
{
if (arr[i] != arr[i+1])
temp[j++] = arr[i]
}
temp[j++] = arr[n-1]
for(int i = 0; i < j; i++)
{
arr[i] = temp[i]
}
return j
}
int removeDuplicate(Node* head)
{
Node* current = head;
Node* temp = NULL;
while(current->next != NULL)
{
if(current->data == current->next->data)
{
temp = current->next->next;
free(current->next)
current->next = temp
}
else
{
current = current->next
}
}
}
bool isPalindrome(string S)
{
for(int i=0; i < S.length()/2; i++)
{
if(S[i] != S[S.length() - i - 1])
{
return false
}
}
return true
}
bool isPalindrome(int arr[], int n)
{
for(int i = 0; i < n/2; i++)
{
if(arr[i] != arr[n-i-1])
{
return false
}
}
return true
}
Node* Insert(Node* root, int data)
{
if (root == NULL) root = GetNewNode(data)
else if (data <= root->data) Insert(root->left, data)
else Insert(root->right, data)
return root
}
bool Search(Node* root, int data)
{
if (root == NULL) return false
else if (data == root->data) return true
else if (data < root->data) return Search(root->left, data)
else return Search(root->right, data)
}
int FindMin(Node* root)
{
Node* current = root
while (current != NULL)
{
current = current->left
}
return current->data
}
int FindMax(Node* root)
{
Node* current = root
while (current != NULL)
{
current = current->right
}
return current->data
}
int height(Node* root)
{
if (root == NULL) return 0
else {
int lHeight = height(root->left)
int rHeight = height(root->right)
if(lHeight > rHeight) return (lHeight + 1)
else return (rHeight + 1)
}
}
int secondLargest(int arr[], int n)
{
int largest = 0, secondL = -1;
for (int i = 1; i < n; i++)
{
if (arr[i] > arr[largest])
largest = i
}
for (int i = 0; i < n; i++)
{
if (arr[i] != arr[largest])
{
if (secondL == -1)
secondL = i
else if (arr[i] > arr[secondL])
secondL = i
}
}
return secondL
}
bool checkPair(int arr[], int size, int x)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = i + 1; j < size; j++)
{
if (arr[i] + arr[j] == sum)
return true
}
}
}
bool checkTriplet(int arr[], int size, int x)
{
for (int i = 0; i < size - 2; i++)
{
for (int j = i + 1; j < size - 1; j++)
{
for (int k = j + 1; k < size; k++)
{
if (arr[i] + arr[j] + arr[k] == x)
return true
}
}
}
}
26- Given a function that generates a random number between 20-50, write a function that generates a random number between 10-70.
function generateRandom(min, max)
{
let difference = max - min
let rand = Math.random()
rand = Math.floor(rand*difference)
rand = rand + min
return rand
}
1) First we will take difference between the limits in which we want the random number
2) We will then multiply the random number with that difference
3) Then we will add the lower limit i.e., min to rand to get a random number between those 2 values
People use recursion only when it is very complex to write iterative code. For example, tree traversal techniques like preorder, postorder can be made both iterative and recursive. But usually we use recursive because of its simplicity.
function findCommonElements3(arr1, arr2) {
const temp = []
arr1.some(item => {if(arr2.includes(item)) temp.push(item)})
return temp
}
console.log(findCommonElements3(array1, array2))
var a = [34, 35, 45, 48, 49];
var b = [48, 55];
var union = [...new Set([...a, ...b])];
console.log(union);
function count(s, c)
{
let res = 0;
for (let i = 0; i < s.length; i++)
{
if (s[i] === c)
res++;
}
return res;
}
function removeDups(arr,n)
{
let mp = new Map();
for (let i = 0; i < n; ++i)
{
if (mp.get(arr[i]) == null)
document.write(arr[i] + " ");
mp.set(arr[i], true);
}
}
void deleteNodeWithoutHead(struct Node* deletingNode) {
if (deletingNode == NULL) {
return;
}
else {
if (deletingNode->next == NULL) {
cout << "Can't delete last node without head" << endl;
return;
}
struct Node* temp = deletingNode->next; // a temp pointer will start pointing the node next to the deleting node
deletingNode->data = temp->data; // replace the data of deleting node with the temp node so that both nodes have the same data now
deletingNode->next = temp->next; // point current node to the node next to temp so that temp can be deleted as both temp and current have same data
free(temp);
}
}