Coding Interview PDF Download
PDF Update History: 5/1/2016, 3/19/2016, 5/1/2015, 12/1/2012
PDF Update History: 5/1/2016, 3/19/2016, 5/1/2015, 12/1/2012
Design and implement a TwoSum class. It should support the following operations: add and find. add – Add the number to an internal data structure. find – Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); add(3); add(5); find(4) -> true find(7) -> false Java Solution … Read more
Problem All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. For example, … Read more
Problem Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you … Read more
This problem is similar to Two Sum. To solve this problem, we can use two pointers to scan the array from both sides. See Java solution below: public int[] twoSum(int[] numbers, int target) { if (numbers == null || numbers.length == 0) return null; int i = 0; int j = numbers.length – 1; … Read more
Problem Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). Follow up: If this function is called many times, how would you optimize it? Related problem: Reverse Integer Java Solution public int reverseBits(int n) { for (int i … Read more
Problem The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, … 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as “one 2, then one 1” or 1211. Given an integer n, generate the nth … Read more
Problem Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most … Read more
Given numRows, generate the first numRows of Pascal’s triangle. For example, given numRows = 5, the result should be: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Java Solution public ArrayList<ArrayList<Integer>> generate(int numRows) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if (numRows <= 0) return result; ArrayList<Integer> pre = new ArrayList<Integer>(); pre.add(1); result.add(pre); for (int … Read more
Problem Write a function that takes an unsigned integer and returns the number of ’1′ bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11′ has binary representation 00000000000000000000000000001011, so the function should return 3. Java Solution public int hammingWeight(int n) { int count = 0; for(int i=1; i<33; i++){ … Read more
Problem Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following is not: 1 / \ 2 2 \ \ 3 3 Java Solution – … Read more
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example: A = [2,3,1,1,4], return true. A = [3,2,1,0,4], return false. Analysis We can … Read more
There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: 1. Each child must have at least one candy. 2. Children with a higher rating get more candies than their neighbors. What is the minimum candies you must … Read more
Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range. Analysis We can … Read more
Problem Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2, return “0.5”. Given numerator = 2, denominator = 1, return “2”. Given numerator = 2, denominator … Read more