r/csinterviewproblems • u/qibag • Jan 10 '16
r/csinterviewproblems • u/parlezmoose • Jan 06 '16
Given a string input that is a math expression using +,- (), and positive integers, return the result of the expression
Example:
Input: "4+(4-(2+1))"
Output: 5Source: Major tech company interview
r/csinterviewproblems • u/parlezmoose • Dec 21 '15
[Topological Sort] Given a list of lexicographically sorted words in an unknown alphabet, return a string of all characters in the alphabet in lexicographic order.
You are given as input a list of words from an unknown alphabet, sorted in lexicographic order. You may assume that the words are all lower case, and do not contain any non-letter characters. For example, using the Roman alphabet, an input might be:
["ad", "art", "bad", "bat", "cat"]
Write a function that accepts such a list as input, and outputs a string containing each character found in the dictionary, sorted in lexicographic order
Examples:
input: ["baa", "abcd", "abca", "cab", "cad"]
output: "bdac"
input: ["caa", "aaa", "aab"]
output: "cab"
For a standard roman alphabet, assuming a complete list of English words, the output should be:
'abcdefghijklmnopqrstuvwxyz'However, we cannot assume that our input words use the standard roman alphabet order.
Source: Google interview and many others.
Edit: My example input and outputs were wrong, updated with new ones. See here for another write up.
r/csinterviewproblems • u/zhay • Dec 20 '15
Find a value in a sorted array of unknown length
You're given an array of integers sorted in ascending order. The length of the array is not known. Assume an exception is thrown if you attempt to read an array value that is out of bounds. Also assume that you don't have access to any properties or methods like: Length, Length(), Size(), Count, length, length(), len(), etc.
Determine if a given integer is present in the array. Determine the run-time complexity of your solution.
r/csinterviewproblems • u/parlezmoose • Dec 19 '15
Sticky: Discussion Thread
Hey coders,
I'm really glad so many people are using this sub. I was not expecting it to be so popular. We are on trending subreddits today! Thanks to everyone submitting questions and answers. I've already been taught some things by other Redditors that I would not have picked up studying on my own. Pretty cool!
I want to reserve the posts for coding problems only, so I figured I'd create this sticky if you want to discuss interview strategies, or make suggestions for the sub, or anything else.
Also, just a reminder: when submitting a question, please try to make sure there are enough details in the text body, along with sample input and output where appropriate. No need to go crazy, but a couple sentences are helpful.
Edit:
Ok, let me lay down this rule in case it's not clear. If you insult people or use rude language you will be banned. Be a fucking adult please.
-parlezmoose
r/csinterviewproblems • u/qibag • Dec 19 '15
Given an array of integers and a partition index, switch the two partitions of the array in O(1) space and O(n) time.
input:
[1,2,3,4,5,6,7], 4output:
[5,6,7,1,2,3,4]input:
[1,2,3,4,5,6,7], 6output:
[7,1,2,3,4,5,6]Source: onsite interview with private B2B company worth a couple hundred million
r/csinterviewproblems • u/qibag • Dec 18 '15
Convert an integer < 4000 into a roman numeral.
input:
78output:
LXXVIIISource: Google phone interview
r/csinterviewproblems • u/zhay • Dec 18 '15
Count number of islands
Given a matrix of 0s and 1s, where 0 represents water and 1 represents land, count the number of islands. Two pieces of land are connected if they are vertically or horizontally touching. Two pieces of land that are diagonal from each other are not touching.
e.g.:
Input
0 1 1 0 0 0 1
0 1 0 0 1 1 0
0 1 1 0 0 1 0
0 0 0 0 0 0 0
0 1 1 1 1 1 0Output
4Bonus: Can you solve this in constant space? Note: the recursive stack is considered extra space, and modifying the matrix at any time is prohibited.
r/csinterviewproblems • • Dec 18 '15
You have 2 analog clocks, both start at the same time of day, one spins clockwise the other anticlockwise, how many times will the backwards clocks time match the normal clocks time?
r/csinterviewproblems • u/qibag • Dec 18 '15
Given a list of numbers between 0-99, print out the missing numbers and number ranges.
Input:
[0,1,5,7,27,54,55,56]Output:
2-4,6,8-26,28-53,57-99Source: Google phone interview
r/csinterviewproblems • u/tyroo • Dec 18 '15
[Easy] Level order traversal of a binary tree
Print out a binary tree level by level
a
/ \
b c
/ \ / \
d e f gwould output
[[a],
[b, c],
[d, e, f, g]]r/csinterviewproblems • u/qibag • Dec 18 '15
Given a list of strings, print out the groups of strings that are permutations of each other in O(n) time.
Input:
aaab
ddss
abaa
dsds
ggOutput:
aaab
abaa
ddss
dsdsSource: Lesser known SV unicorn company
EDIT: it's actually newline separated, I am unfamiliar with reddit formatting.
r/csinterviewproblems • u/pxdra • Dec 18 '15
Evaluate Math Expression
As mentioned in title.
You're given a math expression in a string, return the result as an int.
Example: "10+2*3-5/2" -> 14.
Basic: four basic operations, +-*/ Bonus: parenthesis, power.
r/csinterviewproblems • u/tyroo • Dec 18 '15
[DP] Find all interpretations of a string
Given a map and a string find all interpretations of the string
{ "1": a,
"2": b,
"3": c,
...,
"26": z }
Example 1:
"112" - 3 interpretations
"al", "aab", "kb"
Example 2:
"345" - 1 interpretation
"cdf"Assume the mapping is already given to you and passed in as a parameter
Can be solved in O(n) runtime
Source: Big 4 interview
r/csinterviewproblems • u/qibag • Dec 18 '15
[Linked List] Check if a linked list is a palindrome in O(n) time and O(1) space.
Source: phone interview with SV unicorn company
r/csinterviewproblems • u/CapitalDecay • Dec 18 '15
[Easy] Create an output of a 12 X 12 multiplication table
The first coding job I interviewed for this was the first question they asked me and I was grateful because it was pretty easy. Keep in mind that you should care what the output looks like in the console (the numbers should line up). I only had 10 minutes to complete this and it was pseudo code done in java.
r/csinterviewproblems • u/qibag • Dec 18 '15
Given an out of order list of boarding passes with src -> dest, print out the itinerary.
input: [LAX -> SFO, DCA -> LAX, SEA -> DCA, SFO -> JFK]
output: SEA -> DCA -> LAX -> SFO -> JFK
Source: interview with a well known startup among college students
r/csinterviewproblems • u/parlezmoose • Dec 17 '15
[Algorithm Design] Implement an LRU cache
Implement a cache that uses a least-recently-used algorithm to evict data when necessary. The cache can be initialized with a max size, and should support "get", "set", and "delete" operations.
get(key) //Returns value for "key"
set(key, value) //Sets value for "key"
delete(key) //Removes "key"An item is "used" when a get or set is performed. When the cache is full (size == maxSize) and an insertion is performed, the least recently used item should be evicted.
r/csinterviewproblems • u/parlezmoose • Dec 17 '15
[Hard] Write a function that finds the minimum number of rolls to get to a square on a Snakes and Ladders board
Snakes and ladders is a board game that is apparently popular with interviewers. The game works as follows. there is a board with N squares (say, 30). At each turn you roll a 6 sided die. You then advance the number of squares you rolled. However, at certain positions there are "snakes" and "ladders" which are just lines connecting certain squares to other squares. If you land on a snake you slide down it to the connected square. If you land on a ladder you move up to the square it is connected to.
Write a function that accepts three inputs: the "board" , n1 and n2 where n2 > n1, n1, n2 < N and n1, n2 >= 0. The function should return the minimum number of dice throws required to move from square n1 to square n2.
Part of the problem is deciding how the board should be represented. Think about what data structure would be appropriate...
r/csinterviewproblems • u/parlezmoose • Dec 17 '15
[Arrays] Write a function to permute an array A, given another array O containing the new position of each element
Write a function that accepts as input two arrays "A" and "O". The first array contains some unknown values. The second array "O" contains the new positions to which the values in A should be moved. The function should permute A in place according to the new positions specified by O.
Example inputs:
A: [A, B, C, D, E]
O: [2, 1, 0, 4, 3]
Example output:
A === [C, B, A, E, D]
You may assume array O is always "complete", i.e. that it contains each index once.
Follow ups: what is the running time of your solution?
r/csinterviewproblems • u/parlezmoose • Dec 17 '15
[Strings] Write a function to build a string of length n
Write a function that takes as input a character "s" and an integer "n", and returns a string consisting of char "s" repeated n times. The function should be faster than O(n2 ).
Sample input: "a", 3
Sample output: "aaa"
r/csinterviewproblems • u/parlezmoose • Dec 17 '15
[Linked Lists] Reverse a singly linked linked list in place
Reverse a singly linked list in place, returning the new head node. You may assume there are no cycles. You may assume you are always passed an instance of a "Node" class that contains a reference to the next node.
Sample input: 1 -> 2 -> 3 -> 4
Sample output: 4 -> 3 -> 2 -> 1
r/csinterviewproblems • u/parlezmoose • Dec 17 '15
[Trees and Graphs] Perform an in-order traversal on a binary tree with and without recursion.
Traverse a binary tree in order, printing the value of each node. Do it both with and without using recursion. Follow up: how can your solution be modified to perform pre-order and post-order traversal?
https://en.wikipedia.org/wiki/Tree_traversal#In-order_.28symmetric.29
Sample input:
6
4 8
1 5 7 9Sample output:
1 4 5 6 7 8 9r/csinterviewproblems • u/parlezmoose • Dec 17 '15
[Recursion] Write a function to perform currying addition
Write a function that adds by currying. The function accepts either one numerical argument, or no arguments. If called with a number, the function returns another function. If called without an argument, the function returns the sum of all previous arguments.
Example:
a()
returns 0
a(1)
returns function
a(1)()
returns 1
var add = a(1)(2)(3)
add is a function
add()
returns 6