Taking The Mystery Out Of Technical Interviews

TechIntCourse_Blog

New Course Launch!

I’m ecstatic to announce the launch of Udacity’s newest course: “Technical Interview | Algorithms and Data Structures in Python.” But what is a technical interview, and what specific skills do you need to master, in order to perform well in one?

Let’s start by talking about “the technical interview”. When you’re interviewing for pretty much any job that requires coding, you’ll be asked a mix of behavioral, job-skills, and algorithmic questions. Behavioral questions are meant to assess personality traits, and usually revolve around your actions in past experiences and hypothetical situations. Questions related to job skills focus on your knowledge as it applies directly to the job you’re interviewing for, like your ability to use classifiers if you’re applying for a data science job or whether you really know the Activity lifecycle as an Android developer. Algorithmic questions are meant to provide your potential employer with insight into your thought processes. You’re normally given a vague problem and asked to write code in your language of choice to solve it within a time limit.

Continue Reading

Languages and Libraries for Machine Learning

MLPost_2

In our previous post 5 Skills You Need to Become a Machine Learning Engineer, we identified the key skills you need to succeed in this field. Now, we’re going to address one of the most common questions that comes up from students interested in Machine Learning: Which programming language(s) do I need to know?

The answer may surprise you. It doesn’t really matter!

As long as you’re familiar with the Machine Learning libraries and tools available in your chosen language, the language itself isn’t as important. A variety of Machine Learning libraries are available in different programming languages. Depending on your role within a company, and the task you’re trying to accomplish, certain languages, libraries and tools can be more effective than others.

Continue Reading

Computer Science Fundamentals, and How To Learn Them

The term Computer Science is both simple and self-explanatory (the study of computation), and multi-faceted and vast. Because Computer Science deals in both theory and practice, there are many ways to approach and understand the subject. At Udacity, guided by the principles encapsulated in our “Be In Demand” slogan, we take a career-centric approach that focuses explicitly on mastering skills and concepts which directly apply to launching a career in technology.

As Udacity instructor Dave Evans says of our Intro to Computer Science course:

This course is a first step into the world of computer science, and whether you want to become a software engineer, or collaborate with software engineers, this course is for you.

Continue Reading

4 Ways to Pick Your First Programming Language

If you haven’t picked your first programming language, the programming world is your oyster. Yet with evangelists for every language telling you their language is the best, choosing one to start with can be incredibly overwhelming. We’ve looked at the data for the top ten programming languages in the US (based on IEEE Spectrum data) to help you pick the best language to start with based on your priorities in lifestyle, location, and career potential.

Python is a popular, well-paid language, being versatile enough to be used in many different applications, while Javascript is used widely across the country, and can be a good choice if you don’t want to relocate for a job. Although some newer programming languages, such as Swift, are not included, you shouldn’t discount the growth of their popularity. Career opportunities in iOS development using Swift, similar to Android development using Java, will increase as the field of mobile app development continues to expand.

Continue Reading

Write Your First Python Application

We have successfully set up and installed a LAMP server on our Ubuntu machine, optimized our server, and now it’s time to write our first Python application. We won’t be writing anything too fancy, but we will cover the basics of a Python application, and also talk a bit to either our MySQL or PostgreSQL database.

Throughout this tutorial, I will be using the nano editor in the terminal to write my Python code, but feel free to use whichever editor you please. It should not affect the outcome of this tutorial.

Python uses whitespaces. via Udacity

Also note that unlike many popular programming languages, Python uses whitespace indentation as a delimiter for code blocks, instead of the traditional curly braces or keywords. This allows Python to have extremely readable code, and forces you not to write single-line functions. Keep this in mind as you are writing your own program, as you don’t want to have issues caused by missing line breaks or indentation.

Creating Our First .py File

Navigate to a folder that you would like to create your first Python file in, and create a file called test.py. Now open this file up in the editor and type the following code:

print "Hello, World!"

Save your file, and in the terminal, navigate to wherever you created this file, and run it by calling python test.py. You should see the correct output:

This is a very simple way to write a Python application and see the result. However, what if we want to run the application without typing python test.py every single time? To do that, we can add the following to our code:

#!/usr/bin/python
print "Hello, World!"

That top line allows the file to locate the Python interpreter (which, by default, is in the /usr/bin/ folder). Save the file. Now we need to change the permission of this file so that it is executable by the terminal. Run the following:

chmod +x test.py

Now we can run the program by simply typing:

./test.py

Now, let’s add a variable to our program, as well as a conditional statement. What we want the variable to do is hold our first name (you’ll want to change the name used in this example to your own), and then our conditional statement will check to see whether the name is correct. If it is, the program will say hello. If it isn’t, the program will ask for the correct name.

#!/usr/bin/python

name = "Keenan"

if name == "Keenan":  
  print ("Hello,", name)
else:  
  print "Oh, well what is your name then?"

Let’s run our program and see the output.

You should see something similar to the above. Now let’s edit our program and misspell the name to see whether our condition is really working:

name = "Keenann"

Run the program again, and you should see:

Now we know that the conditional statement is checking the name, and if it’s different from the expected value, the program will ask for the correct name.

Writing a Simple Function

Let’s take our basic conditional statement and turn it into a function. We’ll write our function with a single argument that will be used to store our name. The program will then ask our user whether the name is correct, and the user can answer yes or no (inside of the terminal). Depending on whether the name is correct or incorrect, the program will display a different message.

#!/usr/bin/python

  def checkName(name):  
    checkName = input("Is your name " + name + "? ") 
    
    if checkName.lower() == "yes":    
      print("Hello,", name)  
    else:    
      print("We're sorry about that.")

checkName("Keenan")

You can define your function by using the def keyword, followed by a name for your function (in this example, it’s called “checkName”). Inside of our function, we declare a single argument, which allows us to input information into the function to be used. After that, we ask our user whether the name is correct, which can be accomplished by using the input method in Python.

Next, we write a conditional similar to the one above, checking to see whether the name entered is correct. If it is, we welcome the user. If it’s not, we apologize. The first line of the conditional has the biggest change, and that’s because we are checking what the user has typed in (via the checkName object) to see whether it was yes or no. We also force the user’s input to be lowercase, so that no matter how they type in “yes” (whether it’s yes, YES, yEs, or Yes), it will always be compared the same way.

At the end of our file, we are simply running the function we just created, and passing in our name as the parameter.

We should now be able to run our script and walk through both conditionals:

Let’s take our function a step further and allow our somebody to enter their name if it’s not the one the program expected. This will only take one additional line and one changed line.

#!/usr/bin/python3

def checkName(name):
  checkName = input("Is your name " + name + "? ") 
  
  if checkName.lower() == "yes":    
    print("Hello,", name)  
  else:    
    name = input("We're sorry about that. What is your name again? ")    
    print("Welcome,", name)

checkName("Keenan")

All of our changes lie right after our else: statement. Instead of just apologizing to the user, we assign a new value to our name parameter, which will both apologize and capture the new name for our user. Afterwards, we welcome our new user.

Running the application should look something like:

Creating MySQL Tables and Accessing Them

Now that we’ve written a simple function and used variables, user input, and conditional statements, let’s connect to our MySQL database (installed previously) to store and retrieve information.

Before we start writing any code, we need to ensure that a MySQL driver is installed on our machine. We’ll do this by running the following command:

sudo apt-get install python-mysqldb

However, before we add any Python code to actually interact with MySQL, we need to create a database table. Let’s run MySQL in our terminal (you will have to enter your root MySQL password afterward):

mysql -u root -p

Now let’s create a database in MySQL named “python”:

CREATE DATABASE python;

Let’s ensure that we’re connected to our new database in the terminal:

connect python

Now let’s create a table called “users” with one field for an auto-incrementing ID and one field for a first name:

CREATE TABLE users (
  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
  firstname VARCHAR(30) NOT NULL
);

Your terminal should look similar to this:

mysql-create-tables.png

Now that we have our database and tables, let’s set up our Python script so we can access and modify it.

Opening up test.py, we should add the following code to our file:

#!/usr/bin/python
import MySQLdb

# Setup MySQL Connection
db = MySQLdb.connect(host="localhost", user="root", passwd="YOURPASSWORD", db="python")
cursor = db.cursor()

# Insert a row into our table
cursor.execute("INSERT INTO users (firstname) VALUES ('Keenan')")

# Save changes to database
db.commit()

You can see that after we declare our Python path, we import the Python-MySQLdb tool that we installed earlier. This is necessary so our program can interact with our MySQL database. Our next line is creating an object named db that stores our connection to MySQL. Ensure that each field here uses your correct host, username, password, and database (although the only thing that should be changing on your end is the password).

Next, we create an object named cursor that allows us to actually execute queries in MySQL. After that, we use the cursor object we just created and run a MySQL command to insert a row into the users field. In this case, I’m inserting my first name into the firstname field. We then call db.commit() to ensure that all changes we make to our database are saved.

Now if we save our file and run the following command inside of MySQL in our terminal, we should see:

connect python
SELECT * FROM users;

This means that we have successfully inserted a row into our users table in our database. We now know that our Python application is talking to MySQL.

Creating PostgreSQL Tables and Accessing Them

If we want to interact with a PostgreSQL (abbreviated throughout the article as “Postgres”) database, we will need to install and use a tool call psycopg2. We can do that by typing the following into the terminal:

sudo apt-get install python-psycopg2

We need to run Postgres inside of the terminal so that we can create and access our tables, but we need to create a Postgres user first. We’ll do that by first logging in as the default Postgres user:

sudo su - postgres

Now let’s create a Postgres database named “python”:

createdb python

Let’s ensure that we’re connected to our new database in the terminal:

psql -d python

We now need to create a new user and add them to the database so we can execute commands:

CREATE USER keenan WITH PASSWORD 'password';GRANT ALL PRIVILEGES ON DATABASE "python" to keenan;

Now let’s create a table called “users” with one field for an auto-incrementing ID and one field for a first name:

CREATE TABLE "users" ( 
  id SERIAL PRIMARY KEY, 
  firstname VARCHAR(30) NOT NULL 
);

Your terminal should look similar to this:

Now that we have our database and tables, let’s set up our Python script so we can access and modify it.

Opening up test.py, we should add the following code to our file:

#!/usr/bin/python 
import psycopg2  

# Setup PostgreSQL Connection 
db = psycopg2.connect("host=localhost dbname=python user=keenan password=YOURPASSWORD")
cursor = db.cursor()

# Insert a row into our table 
cursor.execute("INSERT INTO users (firstname) VALUES ('Keenan')") # Save changes to database db.commit()

You can see that after we declare our Python path, we import the Psycopg2 tool that we installed earlier. This is necessary so our program can interact with our Postgres database. Our next line is creating an object named db that stores our connection to Postgres. Ensure that each field here uses your correct host, username, password, and database (although the only thing that should be changing on your end is the password).

Next, we create an object named cursor that allows us to actually execute queries in Postgres. After that, we use the cursor object we just created and run a Postgres command to insert a row into the users field. In this case, I’m inserting my first name into the firstname field. We then call db.commit() to ensure that all changes we make to our database are saved.

Now if we save our file and run the following command inside of Postgres in our terminal, we should see:

su - postgres
psql python << EOF

SELECT * FROM users;
EOF

This means that we have successfully inserted a row into our users table in our database. We now know that our Python application is talking to PostgreSQL.

The Finish Line

Congratulations, you now have a very simple Python application that is communicating with your database, storing variables, getting user input, and running conditional statements. You could further expand your program by asking the user their name and storing that in the database, and possibly adding more complexity to your database.

Optimizing and Fine-Tuning Your LAMP Server

Once you have built a LAMP server on Ubuntu, the next important step is ensuring that every component on the server is optimized and fine-tuned. This will allow your website or web application to run much much quicker, leading to better overall stability.

Previously, we built a LAMP server that consisted of either MySQL or PostgreSQL (and so both are covered here), and Python instead of PHP.

Fine-tuning your LAMP server

Apache

Apache is our web server of choice because it is stable, well-supported, and completely open source. Out of the box, Apache works great for running websites and web applications, but could benefit from removing some unnecessary modules and fine-tuning.

Disable Unnecessary Modules

Apache is designed in a modular fashion so that web servers can be customized for particular needs. Popular modules add the ability to rewrite URLs, provide SSL encryption, and more. Apache comes with many of these modules enabled by default, however, which can mean unnecessary overhead and bloat on your server.

In Ubuntu, you can see a list of enabled modules and a list of all available modules in the /etc/apache2/mods-enabled and /etc/apache2/mods-available directories, respectively. To list all modules or just enabled modules, you can use the following command:

ls /etc/apache2/mods-enabled

list-apache2-modules.png

Note that the modules enabled in the example and the modules you have enabled might look different.

Apache modules can be very finicky, as they often have cross-dependencies, and there is no silver bullet for which modules to disable — you must make that decision on a case-by-case basis. For the purposes of our project, which consists of Apache, MySQL or PostgreSQL, and Python, here are the modules that you can consider disabling:

  • PHP, Perl – Since we are using Python for our web application
  • SSL – If you do not require your server to make secure HTTP connections
  • auth and authz – Only necessary if you are having Apache do authentication work
  • alias – Used for manipulation of URLs, but we’ll be using mod_rewrite instead
  • autoindex – When no index.html is present, displays directory listing
  • cgi – Enables use of CGI scripts
  • negotiation – Matches document with client responsibilities
  • filter – Custom-filters requests
  • version – Allows testing with multiple versions

The syntax to disable a given module is:

sudo a2dismod ssl

Disable Apache2 Module

If you are interested in enabling a module, you can use:

sudo a2enmod ssl

Note that after you enable or disable Apache modules, you will need to run sudo service apache2 restart.

Configuration

Perhaps the most important part of optimizing an Apache server is a proper configuration. If you have a misconfigured Apache server, your website or web application will start to choke under the pressure of a large number of HTTP requests, which will, in turn, begin bringing your entire server down. This happens because every HTTP request to your website or web applications requires RAM, and once your server runs out of RAM, it starts killing off processes that are using the most. This can happen, regardless of your configuration, if you suddenly get hundreds of thousands of HTTP requests every day to your server. But at the very least, we can help mitigate the problem.

To start optimizing our configuration, we want to open up our Apache configuration file in the terminal, using nano, a terminal application that allows you to easily edit files:

nano /etc/apache2/apache2.conf

We’ll now navigate to the “prefork MPM” section, which you can search for by using CMD + W in nano. You should see something like this:

# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves

<IfModule mpm_prefork_module>  
    StartServers 5
    MinSpareServers 5  
    MaxSpareServers 10  
    MaxClients 256  
    MaxRequestsPerChild 10000
</IfModule>

The code above is the configuration for the prefork module in Apache. This module is the most common method for Apache to serve HTTP requests. What we need to calculate now is the value we should have for MaxClients.

To do this, we use the following formula: (Total Memory – Database System Memory) / Size Per Apache process.

In this equation, you will be subtracting total memory either from MySQL memory or from PostgreSQL memory.

To find out these values, let’s run the following commands (thank you to Woktron for this advice):

free -mps aux | grep 'mysql' | awk '{print $6}'ps aux | grep 'postgresql' | awk '{print $6}'ps aux | grep 'httpd' | awk '{print $6}'

Your output should look similar to:

So for my personal server — where I have 1992 MB of available memory, MySQL (or PostgreSQL) is taking up 21 MB, and the average Apache process is 22 MB — the equation would be MaxClients = (1992 - 21) / 22, which gives a value of 90 for MaxClients.

Be sure to restart Apache after making these changes:

sudo service apache2 restart

Also note that if you are interested in the other configuration values in this file (StartServers, MinSpareServers, MaxSpareServers, MaxRequestsPerChild), Graham Dumpleton has a great presentation that explains the differences in each value and how changing them can affect your server.

MySQL

Unlike a lot of tools like Apache or Python, database systems are something that need to be regularly maintained. This is because your database size will likely change over time, as will the number of people querying it. However, we will be using a tool called MySQLTuner, which makes this maintenance easy.

You can install MySQLTuner using the following command:

sudo apt-get install mysqltuner

And you can now run it by typing:

mysqltuner

The script will ask for your MySQL username and password, after which you should get an output that looks similar to this:

MySQLTuner Output

The script will give you a list of variables to adjust, which can be changed in /etc/mysql/my.conf.

Be sure to follow MySQLTuner’s recommendations, though, before making changes:

  • When making adjustments, make tmp_table_size/max_heap_table_size equal
  • Increase table_cache gradually to avoid file descriptor limits

After making changes to the configuration, you will also need to restart MySQL:

sudo service mysql restart

PostgreSQL

Similar to MySQL, for PostgreSQL we will be using a tool called PGTune to help with tuning our configuration. However, unlike MySQLTuner, PGTune is an online tool that allows you to input your hardware settings and primary database use, after which it spits out configuration settings that you can just copy and paste.

Here’s an example of what mine looked like:

PGTune

You can now take those configuration settings and add/replace them in the /etc/postgresql/[version]/main/postgresql.conf file

Python

In regards to Python, there isn’t terribly much that you can do on the server side of things to optimize your Python application much further. However, if you have written or are planning on writing a Python application, I recommend reading the official Python Performance Tips page to help in writing a performant application.

The Finish Line

With that, you should now have a fine-tuned LAMP server that you can deploy Python applications on, but which also isn’t hogging unnecessary resources or choking under the pressure of many HTTP requests.

A Step by Step Guide to Install LAMP (Linux, Apache, MySQL, Python) on Ubuntu

If you want to get started building dynamic web applications on your Ubuntu machine, then chances are you will be wanting to use a LAMP stack to build and possibly deploy your work.

LAMP is an acronym that traditionally stands for “Linux, Apache, MySQL, and PHP,” which is a common server configuration for a lot of web applications. However, for the purposes of this article, we’re going to upgrade slightly to “Linux, Apache, MySQL, and Python,” since we’ll be swapping out PHP for Python as our programming language of choice. Also, in addition to MySQL as our database system, I will show you how to install and set up PostgreSQL, another popular and useful database system.

Step by Step Beginner's Guide to Installing LAMP (Linux, Apache, MySQL, Python)

Linux

This procedure assumes that Ubuntu is already installed on your machine. If you need to install it, here are some very simple instructions on getting started.

After you have Ubuntu up and running, you’ll want to make sure that everything on your system is current. To do that, open the terminal and type in the following commands:

sudo apt-get update

The result should look something like the screenshot below. Note that this and the following screenshots show the end of each command as it is completed, and so do not necessarily illustrate the entire process.

Updating Linux packages

sudo apt-get upgrade

Upgrading Linux packages

Note: You will have to confirm “yes” when running the “upgrade” command.

The first command is going to update the list of packages and their versions on your machine without going through the process of actually installing them. The second command installs the packages. For this reason, you should always run the update command first and then upgrade.

Installing Apache

The very heart of our LAMP stack is a piece of server software called Apache. A web server’s job is to process HTTP requests, which are used to send information across the Internet.

Why are we choosing Apache? Because it’s the world’s most popular web server software, it’s extremely stable, it’s well-supported, and it’s completely open source, which means you don’t have to pay anything for it.

To install Apache, type the following command into the Terminal:

sudo apt-get install apache2

Installing Apache

Voilà! You should now have a running web server on your machine. To check this, go to http://localhost/, where you should see a page saying “It works!”

Apache - It Works!

Installing Dependencies

There’s an additional step you’ll need to take to ensure that Apache and Python play together nicely: installing a tool called mod_wsgi. This is free tool for serving Python applications from Apache. You will also be installing a helper package called python-setuptools.

To install the tool and helper package, type the following command into the Terminal:

sudo apt-get install python-setuptools libapache2-mod-wsgi

Installing Python/Apache dependencies

You’ll need to restart your Apache server for mod_wsgi to load:

sudo service apache2 restart

Choosing Between MySQL and PostgreSQL

Next, you’ll want to install your database system. This can be a point of confusion for many people, because there are so many options out there, but I’m going to cover two of the most popular: MySQL and PostgreSQL. Unsure which to choose? Let’s take a look at each.

MySQL is the more common of the two. It’s relatively easy to install and get working. It’s also very well-supported by third-party tools, as well as by most web hosts. And it’s an extremely fast tool due to the fact that it doesn’t implement the full SQL standard, or as many data types as other solutions (in particular: PostgreSQL). This makes MySQL a great tool to use when writing simple applications that run fast and are easy to set up, but that don’t need to do anything too complex.

PostgreSQL, on the other hand, is a SQL-standards-compliant tool that supports many more data types than MySQL. It’s extremely powerful, and it’s designed to power complex applications. You can achieve much more in PostgreSQL than you can using MySQL. However, PostgreSQL is a bit more complicated to set up, and it’s comparatively less performant for simple operations due to its large feature set.

If you would like to see a full comparison between MySQL and PostgreSQL, DigitalOcean has written a wonderfully detailed article that should help in making the choice.

If you are a beginner and don’t actually understand the differences that have been outlined, just choose either one and play with it. The best way to learn which tools you do and don’t like is to try them out.

Installing MySQL

To install MySQL, type the following into the Terminal:

sudo apt-get install mysql-server

During the process of installing MySQL, you will be prompted to set a root password. This is the password you’ll use for the MySQL install. You can choose to set it now, or you can do so later, within the MySQL shell.

Set MySQL root password

After MySQL has finished installing, it starts automatically. To log into your server, use the following command (you will be prompted for your root password):

mysql -u root -p

Logging into MySQL Server

From there, you can start to play with MySQL and set up new databases and users.

Installing PostgreSQL

To install PostgreSQL, type the following command into the Terminal:

sudo apt-get install postgresql

Installing PostgreSQL

To log into your server, use the following command (you will prompted for your user password):

sudo -i -u postgres

Logging into PostgreSQL Server

After logging into your PostgreSQL server, you can start to set up new databases and roles.

Installing Python

The last step is installing Python. Since Ubuntu is our Linux distribution of choice, we have the luxury of having Python automatically installed for us. If you are curious about what version is currently installed, simply type this command into the Terminal:

python

Python in Ubuntu

The version info will be returned. Other than that, your Python installation should be good to go.

The Finish Line

Congrats! Just like that, you’ve installed an Ubuntu LAMP server. From here, you can start to play with Python, you can see how both MySQL and PostgreSQL store and retrieve data—and you can begin building your very own web applications.