![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
technical interviews
文章平均质量分 68
yuanhisn
这个作者很懒,什么都没留下…
展开
-
Replace wild cards with all possible combinations of zeros and ones using recurs
Replace wild cards with all possible combinations of zeros and ones using recursion.Input String: 0?1?Output: 0010, 0011, 0110, 0111 Solution:public List<String> replaceQuestionMar...原创 2015-01-05 11:12:07 · 78 阅读 · 0 评论 -
10001st prime number
Question:https://projecteuler.net/problem=7By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number?思路可以参考 wiki...原创 2015-01-06 12:03:42 · 195 阅读 · 0 评论 -
Find a duplicate element in an array
Question:Suppose you have an array of 1001 integers. The integers are in random order, but you know each of the integers is between 1 and 1000 (inclusive). In addition, each number appears only on...原创 2015-01-07 06:24:57 · 105 阅读 · 0 评论 -
Number of unique ways that ATM can tender
An atm can only dispense values of $1, $5, $20, and $50. Return the number of unique ways that a $ amount of X can be tendered.($1, $5) is distinct from ($5, $1)Input: 4 Output: 1Input: 6 Output: 3I...原创 2015-01-22 03:28:29 · 66 阅读 · 0 评论 -
Sum of Two Linked Lists - 1
Given two numbers represented by two lists, write a function that returns sum list. The sum list is list representation of addition of two input numbers.Example 1Input: First List: 5->6-&g...原创 2015-01-29 05:53:35 · 61 阅读 · 0 评论 -
Sum of Two Linked Lists - 2
Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers. It is not allowed to modify th...原创 2015-01-29 05:56:02 · 68 阅读 · 0 评论 -
Reservoir Sampling - Choose K Numbers with Uniform Probability
Given that you receive one number at a time(streaming numbers), choose K numbers with uniform probability.题目要求从n个input中random sample k个数 (k<=n)其中n是未知的,input是以online的形式获得的。即给定一个function getInput...原创 2015-01-31 03:51:52 · 83 阅读 · 0 评论 -
Fisher–Yates shuffle - Shuffle an Array in Place
Question: How do you shuffle an array in place?伪代码如下:To shuffle an array a of n elements (indices 0..n-1): for i from n − 1 downto 1 do j ← random integer with 0 ≤ j ≤ i excha...原创 2015-01-31 05:51:34 · 77 阅读 · 0 评论 -
first non-repeating character in a string
Given a string, find the first non-repeating character in it. For example, if the input string is “GeeksforGeeks”, then output should be ‘f’ and if input string is “GeeksQuiz”, then output should be...原创 2015-02-14 03:23:08 · 168 阅读 · 0 评论 -
Find two non-repeating elements in an array
一个int型数组里面所有数都出现两次,只有两个数出现一次。找出这两个数。Solution:public void findTwoUniqueNumbers(int[] A) { int x = 0; for(int i=0; i<A.length; i++) { x ^= A[i]; } int lsb = x & -x; int a = 0, ...原创 2015-02-14 11:52:32 · 65 阅读 · 0 评论 -
链表排序 - Sort LinkedList
这篇文章分析一下链表的各种排序方法。 以下排序算法的正确性都可以在LeetCode的链表排序这一题检测。本文用到的链表结构如下(排序算法都是传入链表头指针作为参数,返回排序后的头指针)struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };...原创 2015-02-16 02:10:19 · 135 阅读 · 0 评论 -
Find all prime numbers up to any given limit
Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be ...原创 2015-02-21 00:49:08 · 96 阅读 · 0 评论 -
[待续]Convert string to palindrome string with minimum insertions
Given a string, find the minimum number of characters to be inserted to convert it to palindrome. For Eg :- ab: Number of insertions required is 1. bab aa: N...原创 2015-02-26 05:47:17 · 191 阅读 · 0 评论 -
Suffix Tree
A Suffix Tree is a data-structure that allows many problems on strings (sequences of characters) to be solved quickly. Iftxt=t1t2...ti...tn is a string, then Ti=titi+1...tn is the suffix of txt that...原创 2015-02-26 05:48:22 · 97 阅读 · 0 评论 -
[待续]Minimum appends to convert string into palindrome
Convert the given string into palindrome with minimum number of appends(at end of the given string). O(n) algorithm will be appreciated.Input:MalayalOutput:MalayalamSee discussions here at Linkedi...原创 2015-02-28 15:12:33 · 165 阅读 · 0 评论 -
[待续]Google电面题 - 最短包含字符串
有一个输入字符,另有一个英文字典,其实就是字符串数组,然后写一个function返回字典里拥有输入字符串里所有字母(非数字或者空格等符号,就是纯字母)的最短的那个字符串。举个例子:输入字符串"SR 456 T",字符串数组里有"SORT",而且是最短的,那么就返回它。 ...原创 2015-02-28 15:13:01 · 51 阅读 · 0 评论 -
Remove duplicates from an unsorted linked list
Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? Solution:public ListNode removeDu...原创 2015-03-04 14:47:15 · 59 阅读 · 0 评论 -
K Sum
2sum的算法复杂度是O(N log N) 因为排序用了N log N以及头尾指针的搜索是线性的,所以总体是O(N log N),好了现在考虑3sum, 有了2sum其实3sum就不难了,这样想:先取出一个数,那么我只要在剩下的数字里面找到两个数字使得他们的和等于(target – 那个取出的数)就可以了吧。所以3sum就退化成了2sum, 取出一个数字,这样的数字有N个,所以3sum的算法复...原创 2015-03-05 03:45:00 · 52 阅读 · 0 评论 -
Find distance between two given keys of a Binary Tree
Find the distance between two keys in a binary tree, no parent pointers are given. Distance between two nodes is the minimum number of edges to be traversed to reach one node from other. The di...原创 2015-03-06 13:11:40 · 94 阅读 · 0 评论 -
Print a Binary Tree Vertical Order (column wise)
This is a Facebook interview question.We have a binary tree, suppose like this:8/ \ 610/ \ / \ 47912/ \35We have to print this binary tree in top-down manner - column wise. Note...原创 2015-03-06 13:16:11 · 91 阅读 · 0 评论 -
k-palindrome
Question:A k-palindrome is a string which transforms into a palindrome on removing at most k characters.Given a string S, and an interger K, print “YES” if S is a k-palindrome; otherwise print “...原创 2015-03-09 10:45:25 · 136 阅读 · 0 评论 -
Longest Increasing Subsequence
一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度。 (讲DP基本都会讲到的一个问题LIS:Longest Increasing Subsequence)面对这样一个问题,我们首先要定义一个“状态”来代表它的子问题, 并且找到它的解。注意,大部分情况下,某个状态只与它前面出现的状态有关, 而独立于后面的状态。我们来找一下“状态”和“状态转移方程”。 假如我们考...原创 2015-03-12 05:14:26 · 46 阅读 · 0 评论 -
Facebook interview - Prime Number Combination Product
给一个质数数组,返回所有可能的product,顺序不管比如给 [2,3,5] 返回 [2,3,5,6,10,15,30]数组中的数如果有重复则需要去重,不允许用set。比如给 [2,2,2] 返回 [2,4,8],顺序不用管。 public List<Integer> combinePrimeProduct(int[] primes) { Arrays.so...原创 2015-03-13 00:18:50 · 99 阅读 · 0 评论 -
Count the number of set bits in an integer
Question: Count the number of set bits in an 32 integer. Method 1:Count the bit one by one, 这显然不好:public int bitCount(int a) { int num = 0; for (int i=0; i<32; ++i) { ...原创 2015-03-13 00:19:10 · 104 阅读 · 0 评论 -
Linkedin Interview - Sum of NestedInteger
Given a nested list of integers, returns the sum of all integers in the list weighted by their depth For example, given the list {{1,1},2,{1,1}} the function should return 10 (four 1's at depth 2,...原创 2015-03-16 00:23:26 · 81 阅读 · 0 评论 -
Google Interview - 从海量数据中找出中位数
有几百亿的整数,分布的存储到几百台通过网络连接的计算机上,你能否开发出一个算法和系统,找出这几百亿数据的中值?就是在一组排序好的数据中居于中间的数。显然,一台机器是装不下所有的数据,也尽量少用网络带宽。我的算法训练挺少的,只能自己谈些自己的心得。1.获得题目,要仔细研读,迅速转化为数学模型。混乱 -->有序 , 局部-->整体。大问题化解为小问题来求解。这时,我们的思路 大概就是出...原创 2015-03-18 12:47:09 · 80 阅读 · 0 评论 -
Facebook interview - Move all zeroes to end of array
Given an array of random numbers, Push all the zero’s of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, ...原创 2015-03-19 03:29:40 · 65 阅读 · 0 评论 -
A crack at Dynamic Programming
From: http://blog.rfaisal.com/2013/05/19/a-crack-at-dynamic-programming/Dynamic Programming is a discrete optimization technique. It means, the variables we want to calculate using this method are...原创 2015-03-19 06:57:07 · 86 阅读 · 0 评论 -
Google Interview - 找出二叉查找树中出现频率最高的元素
找出二叉查找树中出现频率最高的元素。树中结点满足left->val <= root->val <= right->val。如果多个元素出现次数相等,返回最小的元素。在一个有序数组中,我们查找出现频率最高的元素,很简单,顺序扫描一遍即可统计出。那么我们对二叉查找树也可以用类似方式统计,因为中序遍历序列就是有序序列,所以我们在中序遍历的过程中就可以统计出出现频率最...原创 2015-03-20 05:45:51 · 137 阅读 · 0 评论 -
Google Interview - 数字旋转180度
找出1~10^n中数字翻转过来是本身的数( 96 ->96, 18 -> 81, 0 -> 0, 其他数字翻过来都不是数字)设计一个函数,判断某个只包含数字的字符串是不是Ambigram(比如,69,88,609,818等都是Ambigram)。Follow up:给一个整数n,找出所有长为n的Ambigram。可以先求出n-2的答案,再利用n-2的答案求出n的答案,...原创 2015-03-20 05:46:35 · 92 阅读 · 0 评论 -
Facebook&Uber interview - Eval Math Expression
题目就是计算表达式 1+2*3+4 = 11可以去搜parse mathematical expression,不考虑括号 Solution: use two arrays to store numbers and signs. nums = [1,2,3,4] signs = [+, *, +]scan signs, when we meet * or /, ...原创 2015-03-22 05:08:01 · 45 阅读 · 0 评论 -
Convert a BST to a sorted circular doubly-linked list
Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list.If the problem stat...原创 2015-03-22 05:11:18 · 85 阅读 · 0 评论 -
Convert a Binary Tree to Doubly Linked List
Given a Binary Tree (Bt), convert it to a Doubly Linked List(DLL). The left and right pointers in nodes are to be used as previous and next pointers respectively in converted DLL. The order of nodes...原创 2015-03-22 05:11:40 · 62 阅读 · 0 评论 -
Producer Consumer using BlockingQueue
Usage example, based on a typical producer-consumer scenario. Note that a BlockingQueue can safely be used with multiple producers and multiple consumers. class Producer implements Runnable { ...原创 2015-03-24 10:33:38 · 57 阅读 · 0 评论 -
Counting inversions in an array
Count the number of inversion pairs in an array of n numbers. An inversion pair is defined as following: - If i < j and arr[i] > arr[j] then (arr[i], arr[j]) is called inve...原创 2015-03-24 11:00:19 · 102 阅读 · 0 评论 -
Facebook interview - String with Balanced Parentheses
Given a string with parentheses, return a string with balanced parentheses by removing the fewest characters possible. You cannot add anything to the string. Examples: balance("()") -> "()" bala...原创 2015-03-24 11:17:24 · 55 阅读 · 0 评论 -
Egg Dropping Puzzle
Question:There is a building of 100 floors If an egg drops from the Nth floor or above it will break. If it’s dropped from any floor below, it will not break. You’re given 2 eggs. Find N, while m...原创 2015-03-26 02:56:45 · 99 阅读 · 0 评论 -
两个有序数组的交集和并集
两个有序数组的交集和并集。Intersection of two sorted array. (find the common elements between two sorted arrays)Follow up:找出两个有序数组里不同的数字(类似求集合的异或)这是Facebook的电面题。/** * 求解两个有序数组的交集 * @param a...原创 2015-03-27 00:29:38 · 175 阅读 · 0 评论 -
Boundary Traversal of binary tree
Given a binary tree, print boundary nodes of the binary tree Anti-Clockwise starting from the root. For example, boundary traversal of the following tree is “20 8 4 10 14 25 22″We break the problem...原创 2015-03-27 02:22:11 · 62 阅读 · 0 评论 -
Kth Largest Element
Find K-th largest element in an array.NoteYou can swap elements in the arrayExampleIn array [9,3,2,4,8], the 3rd largest element is 4In array [1,2,3,4,5], the 1st largest element ...原创 2015-03-27 04:12:51 · 81 阅读 · 0 评论