Skip to content

Commit ac9c6ad

Browse files
committed
New Commits
2 parents f32bac9 + f25736f commit ac9c6ad

26 files changed

+591
-0
lines changed

Bit-Manipulation/Min-XOR-Value.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''
2+
Given an array of N integers, find the pair of integers in the array which have minimum XOR value. Report the minimum XOR value.
3+
'''
4+
def minXOR(arr):
5+
arr = sorted(arr)
6+
n = len(arr)
7+
minXor = float('inf')
8+
val = 0
9+
for i in range(n-1):
10+
val = arr[i]^arr[i+1]
11+
minXor = min(minXor,val)
12+
13+
return minXor
14+
15+
arr = [0,4,7,9]
16+
print minXOR(arr)

Bit-Manipulation/divide-integers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
# @return an integer
3+
def divide(self, dividend, divisor):
4+
positive = (dividend < 0) ^ (divisor < 0)
5+
dividend, divisor = abs(dividend), abs(divisor)
6+
res = 0
7+
while dividend >= divisor:
8+
temp, i = divisor, 1
9+
while dividend >= temp:
10+
dividend -= temp
11+
res += i
12+
i <<= 1
13+
temp <<= 1
14+
if not positive:
15+
res = -res
16+
return min(max(-2147483648, res), 2147483647)

Bit-Manipulation/number-of-1-bits.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#Approach 1
2+
def setbits(n):
3+
a = bin(n)
4+
a = a[2:]
5+
return a.count('1')
6+
7+
n = 4
8+
print setbits(n)
9+
10+
#Approach 2
11+
def num1bits(n):
12+
res = 0
13+
while n != 0:
14+
if n&1:
15+
res += 1
16+
17+
#Shifting by one bit to the right
18+
n = n >> 1
19+
return res

Bit-Manipulation/reverseBit.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def reversebit(num):
2+
n = '{0:032b}'.format(num)
3+
rev = n[::-1]
4+
return int(rev,2)
5+
6+
num = 4294967295
7+
print reversebit(num)

Bit-Manipulation/single-number.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Given an array of integers, every element appears twice except for one. Find that single one.
3+
4+
Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
5+
6+
Example :
7+
8+
Input : [1 2 2 3 1]
9+
Output : 3
10+
'''
11+
def singlenum(arr):
12+
res = 0
13+
for i in arr:
14+
res ^= i
15+
print res
16+
arr= [1,2,2,3,1]
17+
singlenum(arr)

LinkedList/List-Cycle.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
# @param A : head node of linked list
9+
# @return the first node in the cycle in the linked list
10+
def detectCycle(self, head):
11+
slow = fast = head
12+
while fast and fast.next:
13+
slow = slow.next
14+
fast = fast.next.next
15+
if slow == fast:
16+
break
17+
else:
18+
return None
19+
while head != slow:
20+
slow = slow.next
21+
head = head.next
22+
return head

LinkedList/Merge-Lists.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Merge two sorted linked lists and return it as a new list.
3+
The new list should be made by splicing together the nodes of the first two lists, and should also be sorted.
4+
5+
For example, given following linked lists :
6+
7+
5 -> 8 -> 20
8+
4 -> 11 -> 15
9+
The merged list should be :
10+
11+
4 -> 5 -> 8 -> 11 -> 15 -> 20
12+
'''
13+
def merge(h1,h2):
14+
if None in (h1,h2):
15+
return h1 or h2
16+
dummy = head = ListNode(0)
17+
while h1 and h2:
18+
if h1.val < h2.val:
19+
head.next = h1
20+
head = h1
21+
h1 = h1.next
22+
else:
23+
head.next = h2
24+
head = h2
25+
h2 = h2.next
26+
27+
head.next = h1 or h2
28+
return dummy.next

LinkedList/Palindrome-List.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Given a singly linked list, determine if its a palindrome. Return 1 or 0 denoting if its a palindrome or not, respectively.
3+
4+
Notes:
5+
6+
Expected solution is linear in time and constant in space.
7+
For example,
8+
9+
List 1-->2-->1 is a palindrome.
10+
List 1-->2-->3 is not a palindrome.
11+
'''
12+
def isPalindrome(self,head):
13+
slow = fast = head
14+
while fast and fast.next:
15+
slow = slow.next
16+
fast = fast.next.next
17+
prev = None
18+
while slow:
19+
next = slow.next
20+
slow.next = prev
21+
prev = slow
22+
slow = next
23+
while prev:
24+
if prev.val != head.val:
25+
return 0
26+
prev = prev.next
27+
head = head.next
28+
return 1

LinkedList/Remove-Duplicates.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def removeDuplicates(head):
2+
curr = head
3+
while curr:
4+
while curr.next and curr.next.val == curr.val:
5+
curr.next = curr.next.next
6+
curr = curr.next
7+
return head

LinkedList/Remove-Nth-Node.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def removeNthFromEnd(self, head, n):
9+
fast = slow = head
10+
for _ in range(n):
11+
fast = fast.next
12+
if not fast:
13+
return head.next
14+
while fast.next:
15+
fast = fast.next
16+
slow = slow.next
17+
slow.next = slow.next.next
18+
return head

LinkedList/Reverse-LinkedList-ii.py

Whitespace-only changes.

LinkedList/Sort-List.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''Sort a linked list in O(n log n) time using constant space complexity.'''
2+
def merge(self,h1,h2):
3+
dummy = tail = ListNode(0)
4+
while h1 and h2:
5+
if h1.val < h2.val:
6+
tail.next = h1
7+
tail = h1
8+
h1 = h1.next
9+
else:
10+
tail.next = h2
11+
tail = h2
12+
h2 = h2.next
13+
tail.next = h1 or h2
14+
return dummy.next
15+
def sortList(head):
16+
if head or head.next:
17+
return head
18+
prev, slow, fast = None, head, head
19+
while fast and fast.next:
20+
prev, slow, fast = slow, slow.next, fast.next.next
21+
prev.next = None
22+
return self.merge(self.sort(head),self.sort(slow))

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
# InterviewBit Solutions
66

7+
<<<<<<< HEAD
78
![languages-Python%20&%20C++-orange.svg](https://img.shields.io/badge/languages-Python%20&%20C++-orange.svg) [![License-GNU-red.svg](https://img.shields.io/badge/License-GNU-red.svg)](https://img.shields.io/badge/License-GNU-red.svg) [![codebeat-A-brightgreen.svg](https://img.shields.io/badge/codebeat-A-brightgreen.svg)](https://codebeat.co/projects/github-com-alex-keyes-interviewbit)
9+
=======
10+
![languages-Python%20&%20C++-orange.svg](https://img.shields.io/badge/languages-Python%20&%20C++-orange.svg) [![License-GNU-red.svg](https://img.shields.io/badge/License-GNU-red.svg)](https://img.shields.io/badge/License-GNU-red.svg) [![codebeat-A-brightgreen.svg](https://img.shields.io/badge/codebeat-A-brightgreen.svg)](https://codebeat.co/projects/github-com-alex-keyes-interviewbit)
11+
>>>>>>> f25736fcc5701ad2c7c6fe7a9ef9ce691d7acab1
812
913
This is a repository of solutions to all problems I've solved on InterviewBit. I am not quite sure exactly how many problems there are on the website, but I'll be updating this with every problem I solve. Please issue a pull request if you think you have a better solution or something I could improve upon.
1014

@@ -16,3 +20,8 @@ This is a repository of solutions to all problems I've solved on InterviewBit. I
1620
* [Binary Search](https://github.com/rahulcode22/Data-structures/tree/master/Binary-Search)
1721
* [String](https://github.com/rahulcode22/Data-structures/tree/master/String)
1822
* [Stack and Queue](#)
23+
<<<<<<< HEAD
24+
=======
25+
26+
27+
>>>>>>> f25736fcc5701ad2c7c6fe7a9ef9ce691d7acab1

String/Searching-word-in-2D-matrix.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def findword(matrix,row,col,word):
2+
x = [-1,-1,-1,0,0,1,1,1]
3+
y = [-1,0,1,-1,1,-1,0,1]
4+
#if first vhar of word doesn't match with given starting point in grid
5+
if matrix[row][col] != word[0]:
6+
return False
7+
len_ = len(word)
8+
#searching words in all directions
9+
for dir_ in range(8):
10+
#intialize starting point for curr direction
11+
rd = row + x[dir_]
12+
cd = col + y[dir_]
13+
#first character is already matched,check remaining char
14+
for k in range(1,len_):
15+
if rd >= len(matrix) or rd <0 or cd >= len(matrix[0]) or cd < 0:
16+
break
17+
#if not matched break
18+
if matrix[rd][cd] != word[k]:
19+
break
20+
rd += x[dir_]
21+
cd += y[dir_]
22+
if k == len_:
23+
return True
24+
return False
25+
26+
def patternSearch(matrix,word):
27+
for row in range(len(matrix)):
28+
for col in range(len(matrix[0])):
29+
if findword(matrix,row,col,word):
30+
return row,col
31+
32+
matrix = [['G','E','E','K','S','F'],['Q','U','I','Z','Q','A'],['A','B','C','D','E','F']]
33+
print patternSearch(matrix,"GEEKS")

String/Valid-ip-address.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''Given a string containing only digits, restore it by returning all possible valid IP address combinations.
2+
3+
A valid IP address must be in the form of A.B.C.D, where A,B,C and D are numbers from 0-255. The numbers cannot be 0 prefixed unless they are 0.
4+
5+
Example:
6+
7+
Given “25525511135”,
8+
9+
return [“255.255.11.135”, “255.255.111.35”]. (Make sure the returned strings are sorted in order)'''
10+
def validIpAddress(st):
11+
temp = []
12+
def is_valid(x):
13+
return x and int(x) < 256 and not (s.startwith('0') and len(x) > 1)
14+
for i in range(1,4):
15+
if not is_valid(st[:i]):
16+
continue
17+
for j in range(i,i+4):
18+
if not is_valid(st[i:j]):
19+
continue
20+
for l in range(j,j+4):
21+
if not (is_valid(st[j:l]) and is_valid(st[l:])):
22+
continue
23+
ip = st[:i], st[i:j], st[j:l], st[l:]
24+
temp.append('.'.join(ip))
25+
return sorted(temp)

Two Pointers/3Sum.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
3+
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
4+
Return the sum of the three integers.
5+
6+
Assume that there will only be one solution
7+
8+
Example:
9+
given array S = {-1 2 1 -4},
10+
and target = 1.
11+
12+
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2)
13+
'''
14+
def closestSum(arr,target):
15+
i = 0
16+
arr = sorted(arr)
17+
min_ = float('inf')
18+
n = len(arr)
19+
res= 0
20+
while (i < n):
21+
j = i+1
22+
k = n-1
23+
while (j<k):
24+
sum_ = arr[i]+arr[j]+arr[k]
25+
diff = abs(sum_ - target)
26+
if diff == 0:
27+
return sum_
28+
if diff < min_:
29+
min_ = diff
30+
res = sum_
31+
if sum_ <= target:
32+
j += 1
33+
else:
34+
k -= 1
35+
i += 1
36+
return res
37+
38+
arr = [-1,2,1,-4]
39+
target = 1
40+
print closestSum(arr,target)

Two Pointers/3SumZero.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?
3+
Find all unique triplets in the array which gives the sum of zero.
4+
'''
5+
def check(arr):
6+
i = 0
7+
arr = sorted(arr)
8+
n = len(arr)
9+
lis = []
10+
while i < n:
11+
j = i + 1
12+
k = n - 1
13+
while j < k:
14+
sum_ = arr[i] + arr[j] + arr[k]
15+
if sum_ == 0:
16+
lis.append((arr[i],arr[j],arr[k]))
17+
if sum_ <= 0:
18+
j += 1
19+
else:
20+
k -= 1
21+
i += 1
22+
lis = list(set(lis))
23+
return lis
24+
arr = [-1,0,1,2,-1,-4]
25+
print check(arr)

Two Pointers/Counting-Triangles.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
You are given an array of N non-negative integers, A0, A1 ,…, AN-1.
3+
Considering each array element Ai as the edge length of some line segment, count the number of triangles which you can form using these array values.
4+
5+
Notes:
6+
7+
You can use any value only once while forming each triangle. Order of choosing the edge lengths doesn’t matter. Any triangle formed should have a positive area.
8+
9+
Return answer modulo 109 + 7.
10+
'''
11+
class Solution:
12+
# @param A : list of integers
13+
# @return an integer
14+
def nTriang(self, arr):
15+
arr = sorted(arr)
16+
count = 0
17+
n = len(arr)
18+
if len(arr) < 3:
19+
return 0
20+
for i in range(0,n-2):
21+
k = i+2
22+
for j in range(i+1,n):
23+
while (k < n and arr[i] + arr[j] > arr[k]):
24+
k += 1
25+
count += k - j - 1
26+
return count%(10**9 + 7)

0 commit comments

Comments
 (0)