Show more

Second Freebie!!! This one is marked as "easy"

This problem was asked by Google.

Given two singly linked lists that intersect at some point, find the intersecting node. The lists are non-cyclical.

For example, given A = 3 -> 7 -> 8 -> 10 and B = 99 -> 1 -> 8 -> 10, return the node with value 8.

In this example, assume nodes with the same value are the exact same node objects.

Do this in O(M + N) time (where M and N are the lengths of the lists) and constant space.

Freebie!! Here you go!
This problem was asked by Facebook.

A builder is looking to build a row of N houses that can be of K different colors. He has a goal of minimizing cost while ensuring that no two neighboring houses are of the same color.

Given an N by K matrix where the nth row and kth column represents the cost to build the nth house with kth color, return the minimum cost which achieves this goal.

Here's a freebie. They labeled it as "Hard"

This problem was asked by Google.

Suppose we represent our file system by a string in the following manner:

The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents:

dir
subdir1
subdir2
file.ext
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.

The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents:

dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext
The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext.

We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext", and its length is 32 (not including the double quotes).

Given a string representing the file system in the above format, return the length of the longest absolute path to a file in the abstracted file system. If there is no file in the system, return 0.

Note:

The name of a file contains at least a period and an extension.

The name of a directory or sub-directory will not contain a period.

Okay, this is a big one (description-wise) but basically the idea is to build a Monte Carlo simulation craps simulator to observe strategic differences between "Pass", "Don't Pass", with and without odds.

Here is a description of the necessary craps rules. Ask whatever questions that I haven't made clear.
git.qoto.org/Absinthe/crapssim

This one is super free! It is not a programming problem so much as a BASH problem.

Using bash parameter expansion only gnu.org/software/bash/manual/h

Given an arbitrarily long path, such as "/head/shoulders/knees/and/toes/eyes/ears/mouth" stored in a variable $TEMP construct a parameter expansion to render only the last two subdirectory and filename like "eyes/ears/mouth" such that you can simply offer the command "echo ${TEMP<stuff goes in here>}"

And yes I do know how much easier it is to do with grep, sed, python, perl, awk <insert your favorite alternate solution here> can do it better and easier. This is a real world problem, and someone on Reddit pointed me in the right direction.

I think I may have an idea for the problem this week. Who likes "Craps?" I will work on the specifics, and think of some additional what-ifs as well.

Here's another freebie, but I am not really sure how to solve it. Guess I wouldn't be getting that job at Google :)

This problem was asked by Google.

The area of a circle is defined as πr^2. Estimate π to 3 decimal places using a Monte Carlo method.

Hint: The basic equation of a circle is x2 + y2 = r2.

Here's a fun freebie! Only because I just recently saw a problem similar :)

This problem was asked by Facebook.

Given a stream of elements too large to store in memory, pick a random element from the stream with uniform probability.

Here's another freebie:

This problem was asked by Amazon.

Given an integer k and a string s, find the length of the longest substring that contains at most k distinct characters.

For example, given s = "abcba" and k = 2, the longest substring with k distinct characters is "bcb".

Okay, here is another freebie.

This problem was asked by Amazon.

There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a function that returns the number of unique ways you can climb the staircase. The order of the steps matters.

For example, if N is 4, then there are 5 unique ways:

1, 1, 1, 1
2, 1, 1
1, 2, 1
1, 1, 2
2, 2

What if, instead of being able to climb 1 or 2 steps at a time, you could climb any number from a set of positive integers X? For example, if X = {1, 3, 5}, you could climb 1, 3, or 5 steps at a time.

Here's a freebie,

This problem was asked by Airbnb.

Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative.

For example, [2, 4, 6, 2, 5] should return 13, since we pick 2, 6, and 5. [5, 1, 1, 5] should return 10, since we pick 5 and 5.

Follow-up: Can you do this in O(N) time and constant space?

Okay, maybe you can help me come up with a problem. I can feel it coming on, so here is where I am going:

A trick for pricing things for negotiation is to hide the price encoded. One of the ways of doing this is using a 10 letter isogram such as "upholstery" to convert 0=u, 1=p, 2=h...9=y. So then you could mark a tag or sticker with Tly for $6.49 (with the capitols for dollars and the lower case for cents) Alternatively, you could have an asking price and hide a lowest taking price TlyLuu Meaning that you could ask 6.49 but settle for 4.00. This way someone else could negotiate with the customers, from the person that set the prices.

So writing a program should be unnecessary, the reason for using the isogram is to make it easy to remember and figure it out simply.

So how hard would a program to do this be? Ideas?

This is not a programming question per-se. It, was however, quite interesting and fun to figure out. I am still looking for a new program for this weekend. Unfortunately, no one seemed too interested in last weekend's one, so I may let it float. But I am still looking for one anyway.

An evil warden holds you prisoner, but offers you a chance to escape. There are 3 doors A, B, and C. Two of the doors lead to freedom and the third door leads to lifetime imprisonment, but you do not which door is what type. You are allowed to point to a door and ask a single yes-no question to the warden. If you point to a door that leads to freedom, the warden does answer your question truthfully. But if you point to the door that leads to imprisonment, the warden answers your question randomly, either saying "yes" or "no" by chance. Can you think of a question and figure out a way to escape for sure?


Another Freebie...

This problem was asked by Facebook.

Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded.

For example, the message '111' would give 3, since it could be decoded as 'aaa', 'ka', and 'ak'.

You can assume that the messages are decodable. For example, '001' is not allowed.


Here's another freebie, I assume it is python specific because they start with a base of python code. But if it makes sense, try it in whatever language you like:

This problem was asked by Google.

Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.

For example, given the following Node class

class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
The following test should pass:

node = Node('root', Node('left', Node('left.left')), Node('right'))
assert deserialize(serialize(node)).left.left.val == 'left.left'


Here's a Freebie!

This problem was asked by Stripe.

Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.

For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.

You can modify the input array in-place.

Good morning and here comes the latest challenge.

git.qoto.org/Absinthe/onetonin

The numbers 1 2 3 4 5 6 7 8 9 can have either the operator "+" or "-" placed between them.
As well, if no operator is placed, then the adjoining numbers go together such as 1 2 would
become "12" or 1 2 3 would become "123" and so forth.

The goal is to find the combinations that would make the line calculate up to 100 exactly.

Here is an example: 12 + 3 - 4 + 5 + 67 + 8 + 9 = 100

Would like maybe something other than eval().

I found a corner case on the find sum in list that I overlooked. If the list is [1,2,5] and the number is 4 I will return true because I am looking through the whole list for K-element and will of course find element. I was so happy to get a single line that worked, I am wondering if I can fix it and still keep it one line?

git.qoto.org/Absinthe/sum-of-n

I know count may work better than any, or some other logic to handle the special case of K-element == element

Okay, here is another freebie :) I will put in a real one before the end of the weekend.

Basically, you are given a list from which you need to create a new list where each element is the product of all the "OTHER" elements.

I found a few interesting corner cases.

I challenge you to give it a try!

Read the challenge here:

git.qoto.org/Absinthe/productn

My attempt is checked into that repo as well.

Show more
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.