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
/*
A Discrete Mathematics professor has a class of NN students. Frustrated with their lack of discipline, he decides to cancel class if fewer than KK students are present when class starts.
Given the arrival time of each student, determine if the class is canceled.
Input Format
The first line of input contains TT, the number of test cases.
Each test case consists of two lines. The first line has two space-separated integers, NN (students in the class) and KK (the cancelation threshold).
@gauravbhasin87
gauravbhasin87 / MinAvgWaitTime.java
Created March 13, 2016 02:21
Minimum Average Waiting Time
/*
Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes.
Different kinds of pizzas take different amounts of time to cook. Also, once he starts cooking a pizza, he cannot cook another pizza until the first pizza is completely cooked. Let's say we have three customers who come at time t=0, t=1, & t=2 respectively, and the time needed to cook their pizzas is 3, 9, & 6 respectively. If Tieu applies first-come, first-served rule, then the waiting time of three customers is 3, 11, & 16 respectively. The average waiting time in this case is (3 + 11 + 16) / 3 = 10. This is not an optimized solution. After serving the first customer at time t=3, Tieu can choose to serve the third customer. In that case, the waiting time will be 3, 7, & 17 respectively. Hen
@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)
{
@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 / 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) {
/*
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 / 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
@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 / 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 / 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