Skip to content

Instantly share code, notes, and snippets.

View gauravbhasin87's full-sized avatar
🏠
Working from home

gauravbhasin87

🏠
Working from home
View GitHub Profile
@gauravbhasin87
gauravbhasin87 / DynamicArray.java
Created March 13, 2016 01:44
Dynamic Array - Implementation of Hash Table
/*
There are NN sequences. All of them are initially empty, and you are given a variable lastans=0lastans=0. You are given QQ queries of two different types:
"11 xx yy" - Insert yy at the end of the ((x⊕lastansx⊕lastans) mod NN)th sequence.
"22 xx yy" - Print the value of the (yy mod sizesize)th element of the ((x⊕lastansx⊕lastans) mod NN)th sequence. Here, sizesize denotes the size of the related sequence. Then, assign this integer to lastanslastans.
Note: You may assume that, for the second type of query, the related sequence will not be an empty sequence. Sequences and the elements of each sequence are indexed by zero-based numbering.
The ⊕⊕ symbol denotes the xor operation. You can get more information about it from Wikipedia. It is defined as ^^ in most of the modern programming languages.
Input Format
/* There are NN strings. Each string's length is no more than 2020 characters. There are also QQ queries. For each query, you are given a string, and you need to find out how many times this string occurred previously.
Input Format
The first line contains NN, the number of strings.
The next NN lines each contain a string.
The N+2N+2nd line contains QQ, the number of queries.
The following QQ lines each contain a query string.
Constraints
@gauravbhasin87
gauravbhasin87 / PrintElement.java
Created March 13, 2016 01:49
Print the Elements of a Linked List
/*
If you're new to linked lists, this is a great exercise for learning about them. Given a pointer to the head node of a linked list, print its elements in order, one element per line. If the head pointer is null (indicating the list is empty), don’t print anything.
Input Format
The void Print(Node* head) method takes the head node of a linked list as a parameter. Each struct Node has a data field (which stores integer data) and a next field (which points to the next element in the list).
Note: Do not read any input from stdin/console. Each test case calls the Print method individually and passes it the head of a list.
Output Format
@gauravbhasin87
gauravbhasin87 / InsertNodeTail.java
Created March 13, 2016 01:51
Insert a Node at the Tail of a Linked List
/*
You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node. The given head pointer may be null, meaning that the initial list is empty.
Input Format
You have to complete the Node* Insert(Node* head, int data) method. It takes two arguments: the head of the linked list and the integer to insert. You should not read any input from the stdin/console.
Output Format
Insert the new node at the tail and just return the head of the updated linked list. Do not print anything to stdout/console.
Sample Input
@gauravbhasin87
gauravbhasin87 / InsertNodeHead.java
Created March 13, 2016 01:54
Insert a node at the head of a linked list
/*
You’re given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer, insert this node at the head of the linked list and return the new head node. The head pointer given may be null meaning that the initial list is empty.
Input Format
You have to complete the Node* Insert(Node* head, int data) method which takes two arguments - the head of the linked list and the integer to insert. You should NOT read any input from stdin/console.
Output Format
Insert the new node at the head and return the head of the updated linked list. Do NOT print anything to stdout/console.
Sample Input
@gauravbhasin87
gauravbhasin87 / InsertNodePosition.java
Last active January 20, 2023 01:36
Insert a node at a specific position in a linked list
/*
You’re given the pointer to the head node of a linked list, an integer to add to the list and the position at which the integer must be inserted. Create a new node with the given integer, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty.
Input Format
You have to complete the Node* Insert(Node* head, int data, int position) method which takes three arguments - the head of the linked list, the integer to insert and the position at which the integer must be inserted. You should NOT read any input from stdin/console. position will always be between 0 and the number of the elements in the list (inclusive).
Output Format
Insert the new node at the desired position and return the head of the updated linked list. Do NOT print anything to stdout/console.
Sample Input
/*
You’re given the pointer to the head node of a linked list and the position of a node to delete. Delete the node at the given position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The list may become empty after you delete the node.
Input Format
You have to complete the Node* Delete(Node* head, int position) method which takes two arguments - the head of the linked list and the position of the node to delete. You should NOT read any input from stdin/console. position will always be at least 0 and less than the number of the elements in the list.
Output Format
Delete the node at the given position and return the head of the updated linked list. Do NOT print anything to stdout/console.
Sample Input
@gauravbhasin87
gauravbhasin87 / PreOrder.java
Created March 13, 2016 02:03
Tree: Pre-order Traversal
/*
You are given a pointer to the root of a binary tree; print the values in preorder traversal.
You only have to complete the function.
Input Format
You are given a function,
void Preorder(node *root) {
@gauravbhasin87
gauravbhasin87 / PostOrder.java
Created March 13, 2016 02:05
Tree: Postorder Traversal
/*
You are given a pointer to the root of a binary tree; print the values in post-order traversal.
You only have to complete the function.
Input Format
You are given a function,
void Postorder(node *root) {
@gauravbhasin87
gauravbhasin87 / LeastCommonAncestor.java
Created March 13, 2016 02:19
Binary Search Tree : Lowest Common Ancestor
/*
You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. You only need to complete the function.
Input Format
You are given a function,
node * LCA (node * root ,int v1,int v2)
{