![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
面试题目们
Seth_Lin
这个作者很懒,什么都没留下…
展开
-
[LeetCode]Merge Interval (given list of interval, merge whatever you can merge)
懒得自己写了。。代码这里来的:点击总体来讲就是先按照x来sort,x一样sort y, 然后比较前面的y和后面的xpublic ArrayList<Interval> merge(ArrayList<Interval> intervals) { ArrayList<Interval> res = new ArrayList<Interval>(); if(intervals=原创 2015-04-01 13:29:44 · 361 阅读 · 0 评论 -
[LinkedIn]linked List data structure so that add/insert, remove, get random in O(1) constant time
//ArrayList + HashMap//remove的时候把最后一个元素拿过来补上interface List<T> { public void add(T o); // add to the last public T get(int index); // get the index object public int size(); // return the size pub原创 2015-04-06 14:54:51 · 914 阅读 · 0 评论 -
[LinkedIn]Find top 10 urls / shared links map reduce
From Here Given a large network of computers, each keeping log files of visited urls, find the top ten most visited URLs.Ans: we will just mimic the actions of map-reduce: 1. pre-processing: let R b原创 2015-03-31 14:56:46 · 460 阅读 · 0 评论 -
[linkedin] flatten a doubly multi level linked list
Problem From Here: http://www.geeksforgeeks.org/flatten-a-linked-list-with-next-and-child-pointers/ Given a linked list where in addition to the next pointer, each node has a child pointer, which may原创 2015-04-07 10:50:48 · 766 阅读 · 0 评论 -
[LinkedIn] Smallest character that is strictly larger than the search character target
/** * Return the smallest character that is strictly larger than the search character, * If no such character exists, return the smallest character in the array * @param sortedStr : sorted list of l原创 2015-04-07 04:23:44 · 1396 阅读 · 1 评论 -
[LinkedIn]Isomorphic Strings Dictionary substitution
From Here Given two (dictionary) words as Strings, determine if they are isomorphic.Two words are called isomorphic if the letters in one word can be remapped to get the second word. Remapping a lette原创 2015-03-31 13:42:56 · 436 阅读 · 0 评论 -
[LeetCode]longest common/same prefix
//比较每个位置的字母 string longestCommonPrefix(vector<string> &strs) { string prefix; if(strs.size() ==0) return prefix; int len =0; while(1)原创 2015-04-07 23:29:54 · 442 阅读 · 0 评论 -
[LinkedIn] Smallest/greatest/largest k element in stream of data (integer array) priority Q / heap
//This is for smallest//for largest, use min heap int[] getTopK (int[] input, int k) { if (k <= input.length) return input; PriorityQueue<Integer> q = new PriorityQueue<>(k, new Comparato原创 2015-04-07 12:30:17 · 888 阅读 · 0 评论 -
[LinkedIn] singleton, thread safe
From here//thread safe singleton. static makes it guarantee that it only gets created at the first timepublic class Singleton { public final static Singleton INSTANCE = new Singleton(); private原创 2015-03-31 00:34:18 · 769 阅读 · 0 评论 -
[LeetCode] Insert Interval
这题的解答从这里来的/* * 基本思路就是先扫描走到新的interval应该插入的位置(intervals.get(i).end<newInterval.start),确定new interval的start坐标(取小的)。接下来就是插入新的interval并检查后面是否冲突,一直到新的interval的end小于下一个interval的start,然后取新interval和当前interval中原创 2015-04-07 00:00:29 · 753 阅读 · 0 评论 -
[LeetCode/LinkedIn] Integer to Roman and roman number to Integer
I = 1; V = 5; X = 10; L = 50; C = 100; D = 500; M = 1000; 其中每两个阶段的之间有一个减法的表示,比如900=CM, C写在M前面表示M-C。public String intToRoman(int num) { String roman; String[] symbol = {"M","CM","D","CD",原创 2015-04-06 03:34:10 · 792 阅读 · 0 评论 -
can I win(judge first player to move can win)
/* In "the 100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins. What if w原创 2015-04-08 00:40:06 · 1180 阅读 · 0 评论 -
[LeetCode] Inplace random shuffling an array
public void shuffle(int[] A) { if (A == null || A.length == 0) return; for (int i = 0; i < A.length-1; ++i) { int random = Math.random()*(A.length-i) + i; int temp = A[i];原创 2015-04-04 13:28:30 · 504 阅读 · 0 评论 -
[Amazon ]去元音 get rid of vowels
去元音: http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=105773&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26sortid%3D311//string 是题目给的字符串StringBuffer sb = new StringBuffer();String v转载 2015-08-21 03:24:01 · 1073 阅读 · 0 评论 -
[Amazon] Find loop in a singly linkedlist
As the title sayshttp://stackoverflow.com/questions/2663115/how-to-detect-a-loop-in-a-linked-list Floyd’s cycle-finding algorithm Having a fast and slow pointerboolean hasLoop(Node first) { if(f转载 2015-08-21 02:21:06 · 457 阅读 · 0 评论 -
[LeetCode] Longest Palindromic Substring
// Given a center, either one letter or two letter, // Find longest palindromeprivate String expandCenter(String s, int left, int right) { while (left >= 0 && right < s.length() && s.charAt(left)原创 2015-04-04 13:58:31 · 337 阅读 · 0 评论 -
Find longest covered length of non overlapping interval subsets
Given a list of intervals, find a subset of it that covers the longest length and doesn’t overlap with each otherSolution credit to my brother, Jiasen Linimport java.util.ArrayList;import java.util.Co原创 2015-04-09 09:55:18 · 713 阅读 · 0 评论 -
[LeetCode]Word Break
https://leetcode.com/problems/word-break/Got the solution from here http://blog.csdn.net/linhuanmars/article/details/22358863. Quite clever. Interestingly it kind of use the similar approach from a que原创 2015-04-09 11:15:28 · 419 阅读 · 0 评论 -
[LinkedIn]Serialize (encode) and deserialize(decode) a binary tree
From Here Assume we have a binary tree below: _30_ / \ 10 20 / / \ 50 45 35Using pre-order traversal, the algorithm should write the following to a file:30 10 50 # # # 2原创 2015-03-31 23:12:52 · 1055 阅读 · 0 评论 -
[LinkedIn]Combination factors
From HereLinkedin 的一道题目Keep track of the current remain factor to be decomposed, LargestFactor is the largest possible factor to decompose (so that we don’t need to try anything larger than that).For原创 2015-03-31 11:32:41 · 412 阅读 · 0 评论 -
[LinkedIn] Add Interval (get total covered length)
不多说了,题目如下public interface Intervals { /** * Adds an interval [from, to] into internal structure. */ void addInterval(int from, int to); /** * Returns a total length covered by原创 2015-03-30 22:43:53 · 1978 阅读 · 0 评论 -
reverse a linked list
//in place and in one passListNode *reverse(ListNode *head) { if (!head) return head; ListNode dummy(0); dummy.next = head; ListNode *p = &dummy; while(head->next) { ListNode原创 2015-04-08 00:37:34 · 327 阅读 · 0 评论 -
[LinkedIn] Find all prime numbers below / less than integer n
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-04-07 12:03:15 · 691 阅读 · 0 评论 -
[LeetCode]Maximum number of points on a straight line in 2d plane
From Here Solution: Remember that a line can be represented by y=kx+d, if p1 and p2 are in same line, then y1=x1k+d, y2=kx2+d, so y2-y1=kx2-kx1, so k=(y2-y1)/(x2-x1), then we can apply this for原创 2015-04-01 12:28:30 · 491 阅读 · 0 评论 -
[LinkedIn] Array of products of all other numbers (except/without the current number ) (no division)
Given an array of numbers, nums, return an array of numbers products, where products[i] is the product of all nums[j], j != i. Input : [1, 2, 3, 4, 5] Output: [(2*3*4*5), (1*3*4*5), (1*2*4*5),原创 2015-03-31 23:09:39 · 330 阅读 · 0 评论 -
[LeetCode/LinkedIn] Valid Binary Search Tree
Solution 1. In order Traversalpublic boolean isValidBST(TreeNode root) { ArrayList<TreeNode> list = new ArrayList<TreeNode>(); inOrderBST(root,list); boolean start = true; int prev = 0;原创 2015-04-06 14:13:20 · 560 阅读 · 0 评论 -
[LeetCode] Combination Sum
据说linkedin会考。。。没啥难的倒是public class Solution { public List<List<Integer>> combinationSum2(int[] num, int target) { Arrays.sort(num); List<List<Integer>> ret = new ArrayList<List<Integ原创 2015-03-31 14:12:07 · 232 阅读 · 0 评论 -
[LinkedIn]Merge Sorted Iterator (using comparator)
原帖地址:http://www.mitbbs.com/article_t/JobHunting/32909075.htmlpublic static Iterable<Integer> mergeKSortedIterators(List<Iterator<Integer>> Iters){ Queue<newIter> minHeap = new PriorityQueue<newI原创 2015-03-31 03:12:45 · 916 阅读 · 0 评论 -
[LinkedIn] Find all triangles in an array
From Here Changed it a littlepublic ArrayList<ArrayList<Integer>> valid2(int[] A) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); Arrays.sort(A); f原创 2015-03-31 15:26:46 · 469 阅读 · 0 评论 -
Binary Search Algorithm
Just a BS(bulls**t) algorithm, nothing specialint binaryS (int[] array, int key, int min, int max) { if (max < min) { return -1;//can’t find } int mid = (min + max) / 2; if (arr原创 2015-03-31 15:09:28 · 280 阅读 · 0 评论 -
[LinkedIn]Intersection of two sorted array
print intersection of two sorted array 思路from g4g: 1) Use two index variables i and j, initial values i = 0, j = 0 2) If arr1[i] is smaller than arr2[j] then increment i. 3) If arr1[i] is greater t原创 2015-03-31 03:48:27 · 642 阅读 · 0 评论 -
[LinkedIn] Url shortening
Application layer shortening uses (original url => tiny url)expand url (tiny url => original url)Data Storage (hashtable tiny url’s hash code => original url)原创 2015-03-31 10:28:32 · 1145 阅读 · 0 评论 -
[LinkedIn] Implement a Semaphore
From Here 这是一个带有upperbound的semaphore。public class BoundedSemaphore { private int signals = 0; private int bound = 0; public BoundedSemaphore(int upperBound){ this.bound = upperBound; } p原创 2015-03-31 02:22:12 · 453 阅读 · 0 评论 -
[LeetCode] Pow(x,n) O(logN)
又一个Linkedin面试过的public class Solution { public double pow(double x, int n) { int sign = 1; if(n < 0) { n = -n; sign = -1; } if(n == 0) return原创 2015-03-31 00:01:11 · 334 阅读 · 0 评论 -
[LinkedIn] Find the 100 most frequently occurring words in a set of documents.
CareerCup 看到的解答: 1. Get count of each word. 2. Create a MIN heap of word counts with 1st 100 elements. 3. Now for all other word counts , if count is smaller (OR equal) than root (of max heap), ig原创 2015-03-30 23:37:21 · 475 阅读 · 0 评论 -
[LinkedIn] Reversed Nested Integer
/*** 这道题我加了个变形,reversed的* 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原创 2015-03-30 23:13:00 · 786 阅读 · 0 评论 -
[LinkedIn] Text File Iterable
Implement a (Java) Iterable object that iterates lines one by one from a text file.. /** A reference to a file. */public class TextFile implements Iterable<String>. From 1point 3acres bbs{ public T原创 2015-03-31 13:50:22 · 664 阅读 · 0 评论 -
[LinkedIn]find balance (equilibrium) point (index) in an array (unsorted)
Code From Here (g4g)/**1) Initialize leftsum as 02) Get the total sum of the array as sum3) Iterate through the array and for each index i, do following. a) Update sum to get the right sum.原创 2015-03-31 04:02:21 · 723 阅读 · 0 评论 -
[LinkedIn] Mirror of a binary tree
Given a binary tree, return the mirror of it. Very easy. RecursionTreeNode mirror(TreeNode root) { if (root == NULL) return NULL; else { TreeNode newNode = new TreeNode(root.val); newN原创 2015-03-31 14:25:56 · 917 阅读 · 0 评论 -
[LinkedIn] Minimum Window Substring
From Here /** * Given two nodes of a tree, * method should return the deepest common ancestor of those nodes. * * A * / \ * B C * / \原创 2015-03-31 16:02:16 · 453 阅读 · 0 评论