Icing on the Cake
Our freshly baked cake just came out of the oven, and while the cake is cooling, it's time to make the frosting. We've written some C code to help us figure out how much we'll need:
But when we run this code, we see:
That's unexpected... what's going on here?
Language Transfer
Write the body of a function strReplace(src, old, new) that returns a copy of src but with the first occurrence of old replaced with new. For example:
should result in dst pointing to a new string that contains "My favorite language is C".
What's in a cake?
A cake by any other name would taste as sweet...
Now that we've gotten our icing calculations straightened out, our cake business is doing better than ever.
To keep track of all the cake orders we're getting, we've come up with our very own CakeTable data structure which tracks a cake's identification number and any other information (like who ordered it, any text in the icing).
Here's the interface we have so far:
Next, we'd like to add a way to iterate through all of the cakes in our database. The design team specified that we need to get both the orderNumber and cakeData for each of the cakes as we're iterating through.
You've patiently explained that in C you can only return one value from a function, but they keep sending you sample code that looks like this.
What should our function declaration (not function definition) look like for getNextCake?
Littered Linked Lists
We've got some code that uses LinkedLists. And we just added a helper function to remove an item at a specified index. Problem is, we're getting weird behavior—sometimes the code works and sometimes it doesn't. Even worse, when we put in some debugging code, the problem seems to disappear.
Can you figure out what's going on and how to fix it?
We've got a pretty standard LinkedListNode implementation:
and here's our code to delete nodes:
Double the Fun
We've got an array of doubles—prices for all the different cakes around town. Instead of always having to pass around another variable that tells us the length of the array, we've used a 0.0 terminator to indicate the end of the array. We figured this would make things easier but we're running into some issues with one of our functions.
It should be a simple function that adds up the price of all the cakes:
But we aren't always getting the right answer when we call this function. The totalCost usually ends up way too low and we run out of money on our cake-buying spree. And sometimes we get an absurdly high totalCost. Like, impossibly high, because there's only 25 cakes in there and they shouldn't cost millions of dollars.
Can you figure out what's going wrong in buyAllTheCakes and how to fix it?
Given yesterday's stock prices, find the max profit from one buy and one sell. A classic interview problem, solved in O(n) time and O(1) space. keep reading »
Implement a queue with two stacks. Assume you already have a stack implementation. keep reading »
Figure out which number is repeated. But here's the catch: optimize for space. keep reading »
Figure out which number is repeated. But here's the catch: do it in linear time and constant space! keep reading »
Your friend copied a bunch of your files and put them in random places around your hard drive. Write a function to undo the damage. keep reading »
Efficiently sort numbers in an array, where each number is below a certain maximum. keep reading »
Check whether a string's brackets, braces, and parentheses are properly nested and closed: the valid parentheses problem, solved step by step in O(n) time. keep reading »
Write a recursive function of generating all permutations of an input string. keep reading »
Do an in-place shuffle on an array of numbers. It's trickier than you might think! keep reading »
Find the repeat number in an array of numbers. Optimize for runtime. keep reading »
Given an array of numbers in sorted order, how quickly could we check if a given number is present in the array? keep reading »
Find the rotation point (the smallest item) in a rotated sorted array in O(lg n) time. A classic coding interview problem, with a step-by-step solution. keep reading »
Write a function to tell us if cafe customer orders are served in the same order they're paid for. keep reading »
Given a 7-sided die, make a 5-sided die. keep reading »
Given a flight length and a list of movie lengths, find two movies that add up to exactly the flight length: a real-world two sum problem, in O(n) time. keep reading »
Given an array of integers, return an array where each item is the product of all the others, without division, in O(n) time. Step-by-step solution. keep reading »
Find the highest possible product that you can get by multiplying any 3 numbers from an input array. keep reading »
Merge overlapping meeting time ranges into condensed blocks: the classic merge intervals problem, with a step-by-step O(n lg n) solution. keep reading »
Computer the nth Fibonacci number. Careful--the recursion can quickly spin out of control! keep reading »
Find the area of overlap between two rectangles. In the name of love. keep reading »
Write a function to see if a binary tree is 'superbalanced'--a new tree property we just made up. keep reading »
Write a function that finds the corresponding closing parenthesis given the position of an opening parenthesis in a string. keep reading »
Write code to continually track the max, min, mean, and mode as new numbers are inserted into a tracker class. keep reading »
Given a 5-sided die, make a 7-sided die. keep reading »
You're building a word cloud. Write a function to figure out how many times each word appears so we know how big to make each word in the cloud. keep reading »
A building has 100 floors. Figure out the highest floor an egg can be dropped from without breaking. keep reading »
Check whether a binary tree is a valid binary search tree. Avoid the common gotcha and solve it in O(n) time. A classic interview question, step by step. keep reading »
Write a function to reverse the word order of a string, in place. It's to decipher a supersecret message and head off a heist. keep reading »
Check if any permutation of an input string is a palindrome. keep reading »
Find the kth to last node in a singly linked list in O(n) time and O(1) space, a cousin of removing the nth node from the end. Step-by-step solution. keep reading »
Reverse a singly linked list in place and return the new head, in O(n) time and O(1) space. A classic interview question, solved step by step. keep reading »
Every ID appears twice except one. Find the unique one in O(n) time and O(1) space: the single number problem, with a step-by-step solution. keep reading »
I'm making a new search engine called MillionGazillion(tm), and I need help figuring out what data structures to use. keep reading »
Maximize the value of cakes that fit in a bag when you can take unlimited copies of each: the unbounded knapsack problem, solved step by step. keep reading »
Find the second largest element in a binary search tree. keep reading »
Implement a stack with a get_max method that returns the largest item in O(1) time: the max stack problem, with a step-by-step solution. keep reading »
Write a function to delete a node from a linked list. Turns out you can do it in constant time! keep reading »
Count the number of ways to make an amount of money from a set of coin denominations (the number-of-ways version of coin change). Step-by-step solution. keep reading »
Write a function to reverse a string in place. keep reading »
Design a ticket sales site like Ticketmaster: scope the requirements, model events, seats, and orders, and handle traffic spikes when tickets go on sale. keep reading »
Sign up for updates. Free practice problems every week!
You're in! Now what?
For coding interview prep, there's really no substitute for practice.
Check out our mock coding interview questions. They mimic a real interview by offering hints when you're stuck or you're missing an optimization.
Interview Cake