Skip to content

Instantly share code, notes, and snippets.

@ahmedahamid
ahmedahamid / find-median-sorted-arrays-improved-no-nullables.cs
Last active March 12, 2021 13:52
Median of Two Sorted Arrays | Improved Solution | No Nullables
public class Solution
{
public double FindMedianSortedArrays(int[] A, int[] B)
{
if (Object.ReferenceEquals(A, null) || Object.ReferenceEquals(B, null))
{
throw new ArgumentNullException();
}
int aLen = A.Length;
@ahmedahamid
ahmedahamid / find-median-sorted-arrays-improved.cs
Last active March 10, 2019 17:01
Median of Two Sorted Arrays | Improved Solution
public class Solution
{
public double FindMedianSortedArrays(int[] A, int[] B)
{
if (Object.ReferenceEquals(A, null) || Object.ReferenceEquals(B, null))
{
throw new ArgumentNullException();
}
int aLen = A.Length;
@ahmedahamid
ahmedahamid / find-median-sorted-arrays.cs
Last active March 10, 2019 17:41
Median of Two Sorted Arrays | General Approach
public double FindMedianSortedArrays(int[] A, int[] B)
{
// TODO: Check for corner cases
int aLen = A.Length;
int bLen = B.Length;
int leftHalfLen = GetLeftHalfLength(aLen + bLen);
// aMinCount and aMaxCount are the min and max number
// of values A can contribute to the left half of A ∪ B,
Tests run: 279, Failures: 2, Errors: 0, Skipped: 236, Time elapsed: 17.924 sec <<< FAILURE! - in TestSuite
setUp(com.github.shyiko.mysql.binlog.BinaryLogClientGTIDIntegrationTest) Time elapsed: 0.759 sec <<< FAILURE!
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
@ahmedahamid
ahmedahamid / search-cyclically-sorted-array-EPI.java
Created August 19, 2017 09:55
Useful insights into Binary Search problems
//
// Finds the position of the smallest element in a cyclically sorted array.
// Copied verbatim from: http://elementsofprogramminginterviews.com/solutions/java/BinarySearchCircularArray.java
//
public static int searchSmallest(List<Integer> A) {
int left = 0, right = A.size() - 1;
while (left < right) {
int mid = left + ((right - left) / 2);
if (A.get(mid) > A.get(right)) {
// Minimum must be in A.subList(mid + 1, right + 1).
@ahmedahamid
ahmedahamid / binary-search-JDK-6-b27.java
Created August 19, 2017 09:43
Useful insights into Binary Search problems
private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
#include <string>
#include <fstream>
int main() {
std::ifstream input_file("/etc/passwd");
if (input_file)
{
std::string line;
@ahmedahamid
ahmedahamid / insert_recursive.cpp
Last active September 20, 2015 06:20
Tricky pointer basics explained | Recursive implementation of insertion into binary search tree
tree_node* insert(tree_node* n, int value)
{
if (n == NULL)
{
tree_node* new_node = new tree_node();
new_node->data = value;
new_node->left = new_node->right = NULL;
return new_node;
}
tree_node* insert(tree_node* n, int value)
{
if (n == NULL)
{
return NULL;
}
tree_node* x_parent = NULL;
tree_node* x = n;
@ahmedahamid
ahmedahamid / insert.cpp
Last active September 20, 2015 06:21
Tricky pointer basics explained | Inserting an element into a binary search tree
tree_node* insert(tree_node* n, int value)
{
if (n == NULL)
{
return NULL;
}
tree_node** x = &n;
while ((*x) != NULL)