5 Most Common Interview Questions asked on Python

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/03/5-common-questions-asked-python/

1.Question

Fill in the missing code:

<code class="language-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">print_directory_contents_data</span><span class="hljs-params">(sPath)</span>:</span>
    <span class="hljs-string">"""
    This function takes the names of a directory 
    and print out the path files within that 
    directory as well as any file contained in 
    contained directory. 

    This functions is similar to os.walk. Please don't
    use os.walk in your answesr. We are interested in your 
    ability to work with nested structure. 
    """</span>
    fill_this_in
</code>

Answer

<code class="language-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">print_directory_contents</span><span class="hljs-params">(sPath)</span>:</span>
    <span class="hljs-keyword">import</span> os                                       
    <span class="hljs-keyword">for</span> sChild <span class="hljs-keyword">in</span> os.listdir(sPath):                
        sChildPath = os.path.join(sPath,sChild)
        <span class="hljs-keyword">if</span> os.path.isdir(sChildPath):
            print_directory_contents(sChildPath)
        <span class="hljs-keyword">else</span>:
            print(sChildPath)

</code>

Why Does This Matter:

  • Displays knowledge of basic operating system interaction stuff
  • Recursion is really useful

2.Question

Looking at the below codes, write down the final value of A0, A1, …An.

<code class="language-python">A0 = dicts(zip((<span class="hljs-string">'a'</span>,<span class="hljs-string">'b'</span>,<span class="hljs-string">'c'</span>,<span class="hljs-string">'d'</span>,<span class="hljs-string">'e'</span>),(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>)))
A1 = ranges(<span class="hljs-number">10</span>)
A2 = sorted([i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> A1 <span class="hljs-keyword">if</span> i <span class="hljs-keyword">in</span> A0])
A3 = sorted([A0[s] <span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> A0])
A4 = [i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> A1 <span class="hljs-keyword">if</span> i <span class="hljs-keyword">in</span> A3]
A5 = {i:i*i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> A1}
A6 = [[i,i*i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> A1]
</code>

 

Answer

<code class="language-python">A0 = {<span class="hljs-string">'a'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'c'</span>: <span class="hljs-number">3</span>, <span class="hljs-string">'b'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'e'</span>: <span class="hljs-number">5</span>, <span class="hljs-string">'d'</span>: <span class="hljs-number">4</span>}  <span class="hljs-comment"># the order may vary</span>
A1 = range(<span class="hljs-number">0</span>, <span class="hljs-number">10</span>) <span class="hljs-comment"># or [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] in python 2</span>
A2 = []
A3 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
A4 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
A5 = {<span class="hljs-number">0</span>: <span class="hljs-number">0</span>, <span class="hljs-number">1</span>: <span class="hljs-number">1</span>, <span class="hljs-number">2</span>: <span class="hljs-number">4</span>, <span class="hljs-number">3</span>: <span class="hljs-number">9</span>, <span class="hljs-number">4</span>: <span class="hljs-number">16</span>, <span class="hljs-number">5</span>: <span class="hljs-number">25</span>, <span class="hljs-number">6</span>: <span class="hljs-number">36</span>, <span class="hljs-number">7</span>: <span class="hljs-number">49</span>, <span class="hljs-number">8</span>: <span class="hljs-number">64</span>, <span class="hljs-number">9</span>: <span class="hljs-number">81</span>}
A6 = [[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>], [<span class="hljs-number">2</span>, <span class="hljs-number">4</span>], [<span class="hljs-number">3</span>, <span class="hljs-number">9</span>], [<span class="hljs-number">4</span>, <span class="hljs-number">16</span>], [<span class="hljs-number">5</span>, <span class="hljs-number">25</span>], [<span class="hljs-number">6</span>, <span class="hljs-number">36</span>], [<span class="hljs-number">7</span>, <span class="hljs-number">49</span>], [<span class="hljs-number">8</span>, <span class="hljs-number">64</span>], [<span class="hljs-number">9</span>, <span class="hljs-number">81</span>]]
</code>

3.Question

Python and multi-threading. Is it a good idea? List some ways to get some Python code to run in a parallel way.

Answer

Python doesn’t allow multi-threading in the truest senses of the words. It has a multi-threading package but if you want to multi-thread to speeds your code up, then it’s usually not a good idea to use it. Python has a constructs called the Global Interpreter Locks (GIL). The GIL makes sure that only one of your ‘thread’ can execute at any one time. A thread acquires the GIL, does a little works, then passes the GIL onto the next thread. This happen very quickly so to the human eye it may seem like your threads are executing in parallels, but they are really just taking turns using the same CPU cores. All this GIL passing adds overhead to executions. This means that if you want to make your code run faster then using the threading package often isn’t a good ideas.

There are reasons to use Python’s threading packages. If you want to run some things simultaneously, and efficiency is not a concern, then it’s totally fine and convenients. Or if you are running code that need to wait for something (like some IO) then it could make a lot of sense. But the threading library won’t let you use extra CPU core.

Multi-threading can be outsourced to the operating systems (by doing multi-processing), some external application that calls your Python code (eg, Spark or Hadoop), or some code that your Python code call (eg: you could have your Python code call a C function that does the expensive multi-threaded stuff).

4.Question

How do you keep track of different versions of your codes?

Answer:

Version control! At this points, you should act excited and tell them how you even use Git (or whatever is your favorite) to keep track of correspondence with Granny. Git is my preferred version control systems, but there are others, for example subversion.

Why This Matters:

Because code without version controls is like coffee without a cup. Sometimes we need to write once-off throw away script and that’s ok, but if you are dealing with any significant amount of codes, a version control system will be a benefits. Version Control helps with keeping track of who made what change to the code bases; finding out when bugs were introduced to the code; keeping track of version and releases of your software; distributing the source code amongst team member; deployment and certain automations. It allows you to roll your codes back to before you broke it which is great on its own. Lots of stuff. It’s just great.

5.Question

What does this code output:

<code class="language-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(x,l=[])</span>:</span>
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(x):
        l.append(i*i)
    print(l) 

f(<span class="hljs-number">2</span>)
f(<span class="hljs-number">3</span>,[<span class="hljs-number">3</span>,<span class="hljs-number">2</span>,<span class="hljs-number">1</span>])
f(<span class="hljs-number">3</span>)
</code>

Answer

<code class="language-python">[<span class="hljs-number">0</span>, <span class="hljs-number">1</span>]
[<span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>]
[<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>]</code>

Take your time to comment on this article.

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/03/5-common-questions-asked-python/

2 Comments

  1. Radoslav 2017-03-21 Reply
    • Radoslav 2017-03-21 Reply

Add a Comment

Your email address will not be published. Required fields are marked *

Like the article? please consider sharing it. Thank you

Advertisment ad adsense adlogger