Skip to content

Commit d3f7124

Browse files
committed
Day-60 3 CTCI probs 2
1 parent c1e76e4 commit d3f7124

File tree

6 files changed

+488
-11
lines changed

6 files changed

+488
-11
lines changed

README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 84 |
10-
| Current Streak | 59 days |
11-
| Longest Streak | 59 ( August 17, 2015 - October 14, 2015 ) |
9+
| Total Problems | 87 |
10+
| Current Streak | 60 days |
11+
| Longest Streak | 60 ( August 17, 2015 - October 15, 2015 ) |
1212

1313
</center>
1414

@@ -84,7 +84,10 @@ Include contains single header implementation of data structures and some algori
8484
| Problem 1-9: Given two strings s1 and s2, determine s2 is rotation of s1 using only one call to a function which checks whether one string is rotation of another.|[1-9-string-rotation.cpp](cracking_the_coding_interview_problems/1-9-string-rotation.cpp)|
8585
| Problem 2-1: Remove duplicates from an *unsorted* linked list. What if no temporary buffer is allowed.|[2-1-remove-dups.cpp](cracking_the_coding_interview_problems/2-1-remove-dups.cpp)|
8686
| Problem 2-2: Determine k<sup>th</sup> node from the last of a singly linked list. (Iterative and Recursive Approaches) | [2-2-kthToLast.cpp](cracking_the_coding_interview_problems/2-2-kthToLast.cpp)|
87-
87+
| Problem 2-3: Implement an algorithm to delete a node in the middle of a singly linked list | [2-3-delete-middle-node.cpp](cracking_the_coding_interview_problems/2-3-delete-middle-node.cpp)|
88+
| Problem 2-4: Partition a linked list around a value x, all the nodes smaller than x come before all the nodes greater than equal to x | [2-4-partition.cpp](cracking_the_coding_interview_problems/2-4-partition.cpp)|
89+
| Problem 2-5: You have two numberse represented by a linked list where each node contains a single digit. The digits are stored in reversed order, such that 1's digits are at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.Example: <ul><li> Input: ( 7 --> 1 --> 6 ) + ( 5 --> 9 --> 2 ) that is 617 + 295 </li></ul>
90+
<ul><li> Output: ( 2 --> 1 --> 9 ) i.e. 912. </li><ul> <ul><li> FOLLOW UP : Suppose the lists are stored in forward order, Repeat the above problem.</ul></li><ul><li> Input: ( 6 --> 1 --> 7 ) + ( 2 --> 9 --> 5 ) i.e. 617 + 295 </li> </ul><ul><li> Output: ( 9 --> 1 --> 2 ) i.e. 912 </li></ul><ul><li>Implement it recursively and iteratively.</li></ul> | [2-5-add-lists.cpp](cracking_the_coding_interview_problems/2-5-add-lists.cpp) |
8891
###Dynamic Programming Problems
8992
| Problem | Solution |
9093
| :------------ | :----------: |

cracking_the_coding_interview_problems/2-1-remove-dups.cpp

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/*
22
* Cracking the coding interview edition 6
33
* Problem 2-1 : Remove duplicates from an unsorted linked list.
4-
* Approach 1 : Naive approach of iterating and remove all further duplicates of current node. Space complexity O(1) & time complexity O(n^2)
5-
* Approach 2: Use a hash table
4+
* Approach 1 : Naive approach of iterating and remove all further duplicates of current node.
5+
* Space complexity O(1) & time complexity O(n^2)
6+
* Approach 2: Use a hash table, space complexity O(n), time complexity O(n)
67
*/
78

89

@@ -16,6 +17,11 @@ struct Node {
1617
Node * next = nullptr;
1718
};
1819

20+
/**
21+
* [insert - insert a node at the head of list]
22+
* @param head [head of the list]
23+
* @param data [new node's data]
24+
*/
1925
void insert( Node * & head, int data )
2026
{
2127
Node * newNode = new Node;
@@ -24,15 +30,25 @@ void insert( Node * & head, int data )
2430
head = newNode;
2531
}
2632

33+
/**
34+
* [printList Helper routine to print list]
35+
* @param head [head of the list]
36+
*/
2737
void printList( Node * head ) {
2838
while( head ) {
29-
std::cout << head->data << " ";
39+
std::cout << head->data << "-->";
3040
head = head->next;
3141
}
32-
std::cout << std::endl;
42+
std::cout << "nullptr" << std::endl;
3343
}
3444

3545
//generate a random int between min and max
46+
/**
47+
* [random_range helper routine to generate a random number between min and max (including)]
48+
* @param min [min of range]
49+
* @param max [max of range]
50+
* @return [A random number between min and max]
51+
*/
3652
static inline int random_range(const int min, const int max) {
3753
std::random_device rd;
3854
std::mt19937 mt(rd());
@@ -44,6 +60,10 @@ static inline int random_range(const int min, const int max) {
4460
// Method 1
4561
//space complexity O(1)
4662
// time complexity O(n^2)
63+
/**
64+
* [removeDuplicates Remove duplicates without using extra space]
65+
* @param head [head of list]
66+
*/
4767
void removeDuplicates( Node * head ) {
4868
if ( head == nullptr || ( head && (head->next == nullptr))) {
4969
return;
@@ -65,6 +85,10 @@ void removeDuplicates( Node * head ) {
6585
// Method 2
6686
// space complexity - O(n)
6787
// time complexity - O(n)
88+
/**
89+
* [removeDuplicates1 - Remove duplicates from the list using hash table]
90+
* @param head [head of list]
91+
*/
6892
void removeDuplicates1( Node * head ) {
6993
if ( head == nullptr || ( head && (head->next == nullptr) )) {
7094
return ;
@@ -106,6 +130,5 @@ int main() {
106130
printList(head1);
107131
removeDuplicates1(head1);
108132
printList(head1);
109-
110-
133+
return 0;
111134
}

cracking_the_coding_interview_problems/2-2-kthToLast.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void deleteList( Node * & head ) {
5353
}
5454

5555
/**
56-
* printList - Print the list
56+
* printList - Helper routine to print the list
5757
* @param head - Current head of the list.
5858
*/
5959
void printList( Node * head ) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Cracking the coding interview - edition 6
3+
* Problem 2.3 Delete middle node:
4+
* Implement an algorithm to delete a node in the middle of a singly linked list.
5+
* We are given pointer to that node.
6+
*
7+
* Approach:
8+
* In order to remove a node 'A' from a list, We will need to connect pointer of
9+
* A's previous node to A's next node. Here we don't have access to previous node.
10+
* However, we have pointer to that node, we can copy the data of next node to
11+
* the pointed node and then remove the next node.
12+
* Assumption here is that we are not given last node of the list for deletion.
13+
*/
14+
15+
#include <iostream>
16+
17+
struct Node {
18+
char data;
19+
Node * next;
20+
Node( char c ) : data{ c }, next{ nullptr } { }
21+
};
22+
23+
/**
24+
* [printList - Helper routine to print the list]
25+
* @param head [head of the list]
26+
*/
27+
void printList( Node * head ) {
28+
while( head ) {
29+
std::cout << head->data << "-->";
30+
head = head->next;
31+
}
32+
std::cout << "nullptr" << std::endl;
33+
}
34+
35+
/**
36+
* [deleteNode - delete the "node" from the list]
37+
* @param node [node to be deleted]
38+
*/
39+
void deleteNode( Node * node ) {
40+
if ( node == nullptr || node->next == nullptr ) {
41+
return;
42+
}
43+
Node * nextNode = node->next;
44+
node->data = nextNode->data;
45+
node->next = nextNode->next;
46+
delete nextNode;
47+
}
48+
49+
int main() {
50+
Node * head = new Node('a');
51+
head->next = new Node('b');
52+
head->next->next = new Node('c');
53+
head->next->next->next = new Node('d');
54+
head->next->next->next->next = new Node('e');
55+
std::cout << "List before deletion:\n";
56+
printList(head);
57+
std::cout << "Deleting node containing data as 'c'\n";
58+
deleteNode(head->next->next);
59+
std::cout << "List after deletion:\n";
60+
printList(head);
61+
return 0;
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Cracking the coding interview edition 6
3+
* Problem 2.4 Partition:
4+
* Write code to partition linked list around a value x, such that
5+
* nodes less than x come before all the nodes greater than or equal to x.
6+
* If x is in the list, the values of x only need to be after the elements less
7+
* than x.
8+
* Example
9+
* 3-->5-->8-->5-->10-->2-->1 (x = 5)
10+
* 3-->1-->2-->10-->5-->5-->8
11+
*
12+
* Approach:
13+
* Start with first node, and add every thing bigger or equal to x at tail
14+
* and smaller values at head.
15+
*/
16+
17+
#include <iostream>
18+
#include <random>
19+
20+
struct Node {
21+
int data;
22+
Node * next;
23+
Node( int d ) : data{ d }, next{ nullptr } { }
24+
};
25+
26+
27+
/**
28+
* [insert - helper routine to insert a new node with data]
29+
* @param head [head of the list]
30+
* @param data [data of the new node]
31+
*/
32+
void insert( Node * & head, int data ) {
33+
Node * newNode = new Node(data);
34+
if ( head == nullptr ) {
35+
head = newNode;
36+
} else {
37+
Node * curr = head;
38+
while( curr->next ) {
39+
curr = curr->next;
40+
}
41+
curr->next = newNode;
42+
}
43+
}
44+
45+
/**
46+
* [printList - helper routine to print the list]
47+
* @param head [head of the list]
48+
*/
49+
void printList( Node * head ) {
50+
while ( head ) {
51+
std::cout << head->data << "-->";
52+
head = head->next;
53+
}
54+
std::cout << "nullptr" << std::endl;
55+
}
56+
57+
/**
58+
* [partition - routine to partition list around x]
59+
* @param head [head of the list]
60+
* @param x [data around which partition is being done]
61+
*/
62+
void partition( Node * & head , int x ) {
63+
Node * tail = head;
64+
Node * curr = head;
65+
while( curr != nullptr ) {
66+
Node * nextNode = curr->next;
67+
if ( curr->data < x ) {
68+
//insert at head
69+
curr->next = head;
70+
head = curr;
71+
} else {
72+
// insert at tail
73+
tail->next = curr;
74+
tail = curr;
75+
}
76+
curr = nextNode;
77+
}
78+
tail->next = nullptr;
79+
}
80+
81+
/**
82+
* [random_range helper routine to generate a random number between min and max (including)]
83+
* @param min [min of range]
84+
* @param max [max of range]
85+
* @return [A random number between min and max]
86+
*/
87+
static inline int random_range(const int min, const int max) {
88+
std::random_device rd;
89+
std::mt19937 mt(rd());
90+
std::uniform_int_distribution<int> distribution(min, max);
91+
return distribution(mt);
92+
}
93+
94+
95+
int main() {
96+
Node * head = nullptr;
97+
for ( int i = 0; i < 10; ++i ) {
98+
insert(head, random_range(1,9));
99+
}
100+
std::cout << "List before partition around 5:\n";
101+
printList(head);
102+
partition(head, 5);
103+
std::cout << "List after partition around 5:\n";
104+
printList(head);
105+
return 0;
106+
}

0 commit comments

Comments
 (0)