Skip to content

Commit ae90615

Browse files
committed
new questions added
1 parent 5b320be commit ae90615

File tree

6 files changed

+530
-40
lines changed

6 files changed

+530
-40
lines changed

README.md

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,6 @@ only if there is one number that repeats odd number of times
2222
which has to be returned
2323
- Sometimes if the algo seems to be complicated move to a generalized format where the result is assumed to be N and you are solving it for some x by going from solution to problem, and then try to figure out the algo. (refer question 28.c for more clarification)
2424

25-
### For Linked Lists: (methods that can be applied)
26-
- Use multiple variables to not loose track of the linked list and keep moving them ahead in a manner such that various operations can be done on a linked list
27-
- If you dont want a function to return a value of something, just send that value to the address of the variable by passing the address of variable as argument and accessing it has **
28-
- maintain hash table of addresses of the different nodes (first node has this address and so on) to access the value later without traversing
29-
- maintain multiple pointers. The pointers can be moved at different speeds as per requirements.
30-
- sometimes linked list while designed can have flags to make an algo possible
31-
- If linked list has a loop, and two pointers are taken one moving at double the speed of other, they will meet at some point inside the loop. The distance from the start of the list to the first node where the loop starts is equal to the distance from where they meet to the first node.
32-
- Linked list is generally is used to store large numbers which cannot be stored in an int or is used to store polynomials. If numbers are stored in linked list, you will have to apply your own operations (add, subt and so on for that)
33-
- Use general Data structures like stacks and queues or arrays to sometimes solve the algo.
34-
- Try connecting the end of the linked list to the front or make a loop to solve an algo.
35-
- To make games like snakes & ladders, we can use a linked list with a random pointer, next pointer and data.
36-
Whenever there is a ladder or snake, the random pointer will point there else it will be NULL.
37-
- Consider making additional connections (links to the new list or old list) for traversing or reference point of view when there are multiple things involved (random node eg:). New node sometimes can be added in the middle of the two nodes to maintain a connection and so on.
38-
- Linked list questions mostly can be solved by making connections using ptrs, reversing, having multiple ptrs, using different data structures etc.
39-
- You can traverse a double linked list both ways having a single pointer using XOR operation (question18)
40-
4125
# General hash functions
4226
- take mod with number of elements present
4327

@@ -53,28 +37,6 @@ TODO:
5337
## Note:
5438
**Questions statements are included in the file itself**
5539

56-
### COMING SOON
57-
58-
- merge sort using dynamic programming and hashmaps
59-
- duplicate elimination from an array
60-
- union, intersection and difference of two arrays
61-
- bucket sort
62-
- radix sort for d digit numbers
63-
- find maximum sum subarray from an array
64-
- find triplet that sums to given value using binary search technique
65-
- make a circular linked list with a sentinel
66-
- double linked list insertion all points and deletion
67-
- hash table implementation used in linked list after hashing is done
68-
- program to add, mult, subtract two numbers stored in two different linked list and store the result in another linked list
69-
- storing addresses of a linked list in hash table
70-
- storing addresses of a linked list in stack and doing operations
71-
- hash table implementation of question12 to be done
72-
- use a stack for question13
73-
- heap to solve question15 (merge k sorted linked lists into one)
74-
- question 15 linked list
75-
- merge sort on linked list
76-
- XOR operation double linked list
77-
7840
### General Questions
7941

8042
- [question1.c](/general/question1.c)
@@ -155,14 +117,22 @@ TODO:
155117
-[Insertion and deletion in memory efficient double linked lists](/linked-lists/question18.c)
156118

157119

120+
### Hashing
121+
122+
- [General question to understand linear probing](/hashing/general-question1.c)
123+
- [General question to understand chaining](/hashing/general-question2.c)
124+
- [Check whether given array contains duplicates in k-distance or not](/hashing/question1.c)
125+
- [Check whether two sets given are disjoint or not](/hashing/question2.c)
126+
127+
158128
## Some important concepts to solve algos better
159129

160130
- `XOR` means taking sum of the bits and dividing by two, remainder will be the answer
161131
= 'XOR' is commutative
162132
- 'XOR' of a number with itself is 0
163133
- 'XOR' of zero with a number is the number itself
164-
= '1s compliment is number obtained by reversing all the bits in the binary representation of a number. o to 1 and 1 to 0'
165-
134+
- '1s compliment is number obtained by reversing all the bits in the binary representation of a number. o to 1 and 1 to 0'
135+
- Linear hashing is (h(k) + i)modm (where m is the size of the hash table, h(k) is the hash function that takes the key k and returns a value i is the parameter that is incremented to get different values)
166136

167137
# Topic1: Introduction
168138

hashing/general-program1.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
C program to implement linear probing
3+
*/
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#define LIMIT 30
7+
enum record_status {EMPTY,DELETED,OCCUPIED};
8+
9+
struct Employee{
10+
int employee_id;
11+
int employee_age;
12+
char employee_name[30];
13+
};
14+
15+
struct Record{
16+
struct Employee info;
17+
enum record_status status;
18+
};
19+
20+
int hash_function(int key){
21+
return key%LIMIT;
22+
}
23+
24+
int search_records(int key, struct Record hash_table[]){
25+
int count, temp, position;
26+
temp = hash_function(key);
27+
position = temp;
28+
for(count = 1; count != LIMIT-1; count++){
29+
if(hash_table[position].status == EMPTY){
30+
return -1;
31+
}
32+
if(hash_table[position].info.employee_id == key){
33+
return position;
34+
}
35+
position = (temp + count)%LIMIT;
36+
}
37+
return -1;
38+
}
39+
40+
void delete_records(int key, struct Record hash_table[]){
41+
int position = search_records(key, hash_table);
42+
if(position == -1){
43+
printf("Key not found\n");
44+
}else{
45+
hash_table[position].status = DELETED;
46+
}
47+
};
48+
49+
void display_records(struct Record hash_table[]){
50+
int count;
51+
printf("Hash table\n");
52+
for(count = 0; count<LIMIT; count++){
53+
printf("[%d]:\t", count);
54+
if(hash_table[count].status == OCCUPIED){
55+
printf("Occupied - ID: %d Name: %s Age: %d",hash_table[count].info.employee_id, hash_table[count].info.employee_name, hash_table[count].info.employee_age);
56+
}else if(hash_table[count].status == DELETED){
57+
printf("Position is deleted\n");
58+
}else{
59+
printf("EMPTY\n");
60+
}
61+
62+
}
63+
}
64+
65+
void insert_records(struct Employee emprec, struct Record hash_table[]){
66+
int count, position, temp;
67+
int key = emprec.employee_id;
68+
temp = hash_function(key);
69+
position = temp;
70+
for(count = 1; count != LIMIT-1; count++){
71+
if(hash_table[position].status == EMPTY || hash_table[position].status == DELETED){
72+
hash_table[position].info = emprec;
73+
hash_table[position].status = OCCUPIED;
74+
printf("record inserted into hash table\n");
75+
return;
76+
}
77+
if(hash_table[position].info.employee_id == key){
78+
printf("Duplicate record cannot be inserted\n");
79+
return;
80+
}
81+
position = (temp + count)%LIMIT;
82+
}
83+
printf("hash table limited exceeded\n");
84+
}
85+
86+
int main(){
87+
int count, key, option;
88+
struct Record hash_table[LIMIT];
89+
struct Employee emprec;
90+
for(count = 0;count<LIMIT;count++){
91+
hash_table[count].status = EMPTY;
92+
}
93+
while(1){
94+
printf("1. Insert a Record\n");
95+
printf("2. Delete a Record\n");
96+
printf("3. Search a Record\n");
97+
printf("4. Display all Record\n");
98+
printf("5. Exit\n");
99+
printf("Enter your option\n");
100+
scanf("%d",&option);
101+
switch(option){
102+
case 1: printf("Enter employee ID\n");
103+
scanf("%d",&emprec.employee_id);
104+
printf("Enter employee name\n");
105+
scanf("%s",emprec.employee_name);
106+
printf("Enter employee age\n");
107+
scanf("%d",&emprec.employee_age);
108+
insert_records(emprec,hash_table);
109+
break;
110+
111+
case 2: printf("Enter the key to delete\n");
112+
scanf("%d", &key);
113+
delete_records(key, hash_table);
114+
break;
115+
116+
case 3: printf("Enter the key to search\n");
117+
scanf("%d",&key);
118+
count = search_records(key, hash_table);
119+
if(count == -1){
120+
printf("Record not found\n");
121+
}else{
122+
printf("Record found at Index %d ", count);
123+
}
124+
break;
125+
126+
case 4: display_records(hash_table);
127+
break;
128+
case 5: exit(1);
129+
}
130+
}
131+
return 0;
132+
}

hashing/general-program2.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
C program to implement hashing using chaining
3+
*/
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
9+
struct hash *hashTable = NULL;
10+
int eleCount = 0;
11+
12+
struct node{
13+
int key,age;
14+
char name[100];
15+
struct node *next;
16+
};
17+
18+
struct hash{
19+
int count;
20+
struct node *head;
21+
};
22+
23+
void printHash(struct hash *hashTable, int n){
24+
//array of structures since its contiguous.
25+
//hashTable is a pointer to this contiguous memory so it can access it randomly.
26+
}
27+
28+
struct node *createNode(int key, char *name, int age){
29+
struct node *newnode;
30+
newnode = (struct node *)malloc(sizeof(struct node));
31+
newnode->key = key;
32+
newnode->age = age;
33+
strcpy(newnode->name, name);
34+
newnode->next = NULL;
35+
return newnode;
36+
}
37+
38+
void insertToHash(int key, char *name, int age){
39+
int hashIndex = key%eleCount;
40+
struct node *newnode = createNode(key,name,age);
41+
if(!hashTable[hashIndex].head){
42+
hashTable[hashIndex].head = newnode;
43+
hashTable[hashIndex].count = 1;
44+
return;
45+
}
46+
newnode->next = (hashTable[hashIndex].head);
47+
hashTable[hashIndex].head = newnode;
48+
hashTable[hashIndex].count++;
49+
return;
50+
}
51+
52+
void searchInHash(int key){
53+
int hashIndex = key%eleCount, flag=0;
54+
if(!hashTable[hashIndex].head){
55+
printf("element not found\n");
56+
return;
57+
}
58+
struct node *t = hashTable[hashIndex].head;
59+
while(t){
60+
if(t->key==key){
61+
flag =1;
62+
printf("VoterID : %d\n", t->key);
63+
printf("Name : %s\n", t->name);
64+
printf("Age : %d\n", t->age);
65+
break;
66+
}
67+
t=t->next;
68+
}
69+
if (!flag)
70+
printf("Search element unavailable in hash table\n");
71+
return;
72+
}
73+
74+
int main(){
75+
76+
int n,ch,key,age;
77+
char name[100];
78+
printf("Enter the number of elements\n");
79+
scanf("%d", &n);
80+
eleCount = n;
81+
/*create hash table with n number of buckets*/
82+
hashTable = (struct hash *)calloc(n,sizeof(struct hash));
83+
while(1){
84+
printf("1. Insertion\n 2.Deletion\n 3.Searching \n 4. Display\n 5. Exit\n");
85+
printf("Enter your choice\n");
86+
scanf("%d",&ch);
87+
switch(ch){
88+
case 1: printf("Enter the key value:\n");
89+
scanf("%d", &key);
90+
getchar();
91+
printf("Name:\n");
92+
fgets(name, 100, stdin);
93+
name[strlen(name) - 1] = '\0';
94+
printf("AGE\n");
95+
scanf("%d", &age);
96+
insertToHash(key,name,age);
97+
break;
98+
case 3:
99+
printf("Enter the key to search\n");
100+
scanf("%d",&key);
101+
searchInHash(key);
102+
break;
103+
}
104+
}
105+
printHash(hashTable, n);
106+
return 0;
107+
}

hashing/question1.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Check whether given array contains duplicates in k-distance or not
3+
4+
METHOD1:
5+
For every element in the array you can traverse the next k elements and find if there are any duplicates
6+
Suppose array has N elements
7+
Time complexity: O(kN)
8+
Space complexity: O(1)
9+
10+
METHOD2:
11+
12+
*/
13+
14+
#include <stdio.h>
15+
#include <stdlib.h>
16+
17+
struct hash{
18+
int count;
19+
struct node *head;
20+
};
21+
22+
struct node{
23+
int value;
24+
struct node *next;
25+
};
26+
27+
int searchInHash(struct hash *hashTable, int key, int size){
28+
int hashIndex = key%size;
29+
if(!hashTable[hashIndex].head){
30+
return 0;
31+
}
32+
33+
struct node *t = hashTable[hashIndex].head;
34+
while(t){
35+
if(t->value==key){
36+
return 1;
37+
}
38+
t=t->next;
39+
}
40+
41+
return 0;
42+
}
43+
44+
void insertNewElement(int key,struct node *hashTable, int oldKey, int size){
45+
46+
int hashIndex = key%size;
47+
//search and then delete
48+
//and then insert new one
49+
50+
}
51+
52+
int findDuplicates(int arr[],int size, int k){
53+
struct hash *hashTable = (struct hash *)calloc(k,sizeof(struct hash));
54+
for(int i=2; i<size;i++){
55+
if(searchInHash(hashTable, arr[i],k)){
56+
return 1;
57+
break;
58+
}
59+
insertNewElement(arr[i], hashTable, arr[i-2],k);
60+
}
61+
return 0;
62+
}
63+
64+
int main(){
65+
int arr[] = {1,1,2,3,4,5};
66+
int size= sizeof(arr)/sizeof(arr[0]);
67+
int k=2;
68+
if(!findDuplicates(arr, siz, k)){
69+
printf("no duplicates were found\n");
70+
}else{
71+
printf("duplicates present\n");
72+
}
73+
return 0;
74+
}

0 commit comments

Comments
 (0)