Skip to content

Instantly share code, notes, and snippets.

@korniichuk
Last active May 21, 2021 16:38
Show Gist options
  • Save korniichuk/b5b795bc2cebf6abbb3697057eba7b1e to your computer and use it in GitHub Desktop.
Save korniichuk/b5b795bc2cebf6abbb3697057eba7b1e to your computer and use it in GitHub Desktop.
Solution for Google's phone interview 2021 (Python)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Google phone interview 2021"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Implement the `GooglePhotos` Python 3 class. Where `ack()` method (acknowledge) register an ID, if the ID is the new and higher than `first_id`. The `unask_id()` method will return the first available ID.\n",
"```python\n",
"class GooglePhotos:\n",
"\n",
" def __init__(self, first_id):\n",
" pass\n",
"\n",
" def ack(self, photo_id):\n",
" pass\n",
"\n",
" def unack_id(self):\n",
" pass\n",
"```\n",
"\n",
"Example:\n",
"```python\n",
"client = GooglePhotos(first_id=100)\n",
"client.ack(101)\n",
"client.unack_id() --> 102\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class GooglePhotos:\n",
"\n",
" def __init__(self, first_id):\n",
" self.first_id = first_id\n",
" self.register = []\n",
" self.register.append(first_id)\n",
"\n",
" def ack(self, photo_id):\n",
" if (photo_id > self.first_id) and (photo_id not in self.register):\n",
" self.register.append(photo_id)\n",
"\n",
" def unack_id(self):\n",
" for i in range(self.first_id, max(self.register)+2):\n",
" if i not in self.register:\n",
" return i"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client = GooglePhotos(first_id=2)\n",
"client.register"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client.unack_id() # 3"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client.ack(1)\n",
"client.register"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client.unack_id() # 3"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"client.ack(3)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client.unack_id() # 4"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 3, 5]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client.ack(5)\n",
"client.register"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client.unack_id() # 4"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 3, 5, 4, 6]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client.ack(4)\n",
"client.ack(5)\n",
"client.ack(6)\n",
"client.register"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client.unack_id() # 7"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment