Skip to content

Instantly share code, notes, and snippets.

View charlespunk's full-sized avatar

charlespunk charlespunk

View GitHub Profile
A magic index in an array A[1 ... n-1] is defined to be an index such that A[i] = i. Given a sorted array of distinct integers, write a
method to find a magic index, if one exists, in array A.
FOLLOW UP
What if the values are not distinct.
Imagine a robot sitting on the upper left corner of an X by Y grid. The robot can only move in two directions: right and down. How many
possible paths are there for the robot to go from (0, 0) to (X, Y)?
FOLLOW UP
Imagine certain spots are "off limits", such that the robot cannot step on them. Design an algotithm to find a path for the robot from the
top left to the bottom right.
A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how
many posible ways the child can run up the stairs.
//reverse singly linked list
public static Node reverseList(Node root){
if(root == null) return null;
return reverseListHelp(root);
}
public static Node reverseListHelp(Node root){
if(root.next == null) return root;
Node end = reverseListHelp(root.next);
root.next.next = root;
Implement a function to check if a linked list is a palindrome.
Given a circular linked list, implement an algorithm which returns the node at the begining of the loop.
/*
Given three sorted interger arrays, A, B and C, find three indices (i,j,k) such that max(abs(A[i] - B[j]), abs(A[i]-C[k]), abs(B[j]-C[k])) is minimized.
Implement the function to return the minimal value.
Example:
A: [1,2,4,8,16,32]
B: [3,5,9,15,17,40]
C: [6,13,23,36,45]
_ _ _ I t _ _ _ _ i s _ _ a _ a p p l e _ _ => I t _ i s _ a _ a p p l e
You have two numbers represented by a linked list, where each node contains a single digit. The digits are stores in reverse order, such
that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
FOLLOW UP
Suppose the digits are stored in forward order. Repeat the above problem.
Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x.