Skip to content

Commit f5c03be

Browse files
committed
new question to be added
1 parent ae90615 commit f5c03be

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@ 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+
39+
### For Hashing: (methods that can be applied)
40+
- When solving questions divide value with the size of the hashTable. Keep the size of the hash table one
41+
greater than size of elements to be stored.
42+
- Solve using chaining or linear probing
43+
- Heap, BBST-AVL are some popular data structures that can be also used for questions involving hash table.
44+
- Binary search is very widely used. Can only be applied if the length of array is known and if array is sorted.
45+
2546
# General hash functions
2647
- take mod with number of elements present
2748

@@ -123,6 +144,7 @@ TODO:
123144
- [General question to understand chaining](/hashing/general-question2.c)
124145
- [Check whether given array contains duplicates in k-distance or not](/hashing/question1.c)
125146
- [Check whether two sets given are disjoint or not](/hashing/question2.c)
147+
- [Group all the occurences of the elements order by their first occurence](/hashing/question3.c)
126148

127149

128150
## Some important concepts to solve algos better

hashing/question2.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Space complexity: O(m) OR O(n)
3131
*/
3232

3333
//METHOD2 will be done later
34-
===============================================================================================
34+
//===============================================================================================
3535
//METHOD3
3636
#include <stdio.h>
3737
#include <stdlib.h>

hashing/question3.c

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Group all the occurences of the elements order by their first occurence
3+
4+
METHOD1:
5+
Just like quick sort, make an outer for loop to pick one element and then make an inner one to find the
6+
same element again. Once the element is found, shift all the elements to the right till the place where
7+
the element was found to make space for this element to group it. Repeat these steps for all elements
8+
Time complexity: O(n^2)
9+
Space complexity: O(1)
10+
11+
METHOD2:
12+
Make a BBST-AVL. This tree will contain value of the element, and its frequency (no of times it has occured). Mkae
13+
this tree by traversing the array. Now we will make a new array by visiting the binary search tree.
14+
Now we will traverse the array, search value in the tree and basis the frequency we will repeat that element
15+
in the new array that number of times and delete the element from the BBST. Deleting is imp so next time
16+
when you visit this element in the array you do not repeat it as it has been grouped already
17+
Time complexity: O(n) + O(nlogn) + O(nlogn) = O(nlogn)
18+
Space complexity: O(n) (extra space for hashtable and output)
19+
20+
METHOD3:
21+
Applying the above method using a hashtable
22+
Time complexity: O(n)
23+
Space complexity: O(n) (for hashTable and output array)
24+
*/
25+
26+
//METHOD2 to be done later
27+
//================================================================================================
28+
//METHOD3
29+
30+
#include <stdio.h>
31+
#include <stdlib.h>
32+
33+
struct hash{
34+
int count;
35+
struct node *head;
36+
};
37+
38+
struct node{
39+
int key;
40+
struct node *next;
41+
}
42+
43+
struct hash *hashTable = NULL;
44+
45+
struct node *createNode(int key){
46+
struct node *newnode = (struct node *)malloc(sizeof(struct node));
47+
newnode->next = NULL;
48+
newnode->key = key;
49+
return newnode;
50+
}
51+
52+
void insertInHash(int key, int size){
53+
int hashIndex = key%size;
54+
struct node *newnode = createNode(key);
55+
if(!hashTable[hashIndex].head){
56+
hashTable[hashIndex].head = newnode;
57+
return;
58+
}
59+
newnode->next = hashTable[hashIndex].head;
60+
hashTable[hashIndex].head = newnode;
61+
return;
62+
}
63+
64+
void groupByElements(int arr[], int size){
65+
hashTable = (struct hash *)calloc(size+1,sizeof(struct hash));
66+
int hashSize = size+1;
67+
for(int i=0;i<size;i++){
68+
insertInHash(arr[i], hashSize);
69+
}
70+
71+
//create a new empty array and search in hash and make elements as per that TODO
72+
73+
}
74+
75+
int main(){
76+
int arr[] = {3,2,4,5,6,2,3,5,6,5};
77+
int size = sizeof(arr)/sizeof(arr[0]);
78+
79+
groupByElements(arr,size);
80+
81+
return 0;
82+
}

nextquestions.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121
- XOR operation double linked list
2222
- question1 on hashing
2323
- binary search method to be done on hashing for question2
24-
- BBST for question2 hashing
24+
- BBST for question2 hashing
25+
- BBST for question3 hashing and first method to be done
26+
- how to accurately find the size of the hashTable

0 commit comments

Comments
 (0)