r/learnpython • • Apr 10 '19

My recent interview experience on Python.

Role: Automation Test Developer.

Questions asked:

1: Basics of Linux, few tricky commands, what are inodes, setting up networking etc etc.

2: Python:

Given a file, write a function which accepts a version 
number, date and returns pass or fail. 

 EDIT:
 the file structure is basically a test result log file which contains lines of information.
 Each line has a version and date, tests run and results. 
 I was supposed to return results which are pass fail based on the version number and date given.:)


Reverse a sentence without using slicing.

List comprehension basics. 

What I did right: I wrote enough code solving pythonic interview problems, and I was comfortable writing the code in the interview.

what I did wrong: Focused too much on interview type problems, like Coin change, linked list etc etc. Should have also focused on some real use cases of python. For example, how to test a code with different inputs, how to parse few lines from a file, how to schedule a cron job etc etc.

TLDR; Don't just bust your head trying to solve interview challenges from cracking the coding interview. Also learn to make some real world programs.

412 Upvotes

83 comments sorted by

View all comments

3

u/roryokane Apr 11 '19 edited Apr 11 '19

Given a file, write a function which accepts a version number, date and returns pass or fail. […]

I gave solving this a go. I had to assume some details of the file format and desired output, but even if my assumptions are wrong, solving the problem I did still forced me to learn a decent amount of Python techniques. Given a file test_log.txt with these lines:

v1.0 2019-04-10 4 fail
v1.0 2019-04-10 4 pass
v0.9 2019-04-10 6 pass
v1.0 2019-04-10 3 pass
v1.0 2019-04-09 3 pass
v1.0 2019-04-09 2 pass
v1.0 2019-04-09 2 fail
v0.9 2019-04-09 6 pass
v0.9 2019-04-08 6 pass
v0.9 2019-04-07 5 fail

Running python main.py v1.0 2019-04-10 in the same directory will output this:

Filtering to results for version v1.0 on date 2019-04-10
Passing lines:
v1.0 2019-04-10 4 pass
v1.0 2019-04-10 3 pass
Failing lines:
v1.0 2019-04-10 4 fail

Where main.py contains this Python code:

import sys

pass_lines = []
fail_lines = []

target_version = sys.argv[1]
target_date = sys.argv[2]
print(f"Filtering to results for version {target_version} on date {target_date}")

with open('./test_log.txt') as log_file:
    for line in log_file:
        (version, date, _num_tests, pass_or_fail) = line.split()
        if not (version, date) == (target_version, target_date):
            continue
        
        line = line.rstrip()
        if pass_or_fail == "pass":
            pass_lines.append(line)
        elif pass_or_fail == "fail":
            fail_lines.append(line)
        else:
            print(f"Invalid test result status: #{line}", file=sys.stderr)

print("Passing lines:")
for line in pass_lines:
    print(line)

print("Failing lines:")
for line in fail_lines:
    print(line)

As a bonus, here is the equivalent program in the Ruby programming language. I actually wrote this version first since I know Ruby better than Python.

pass_lines = []
fail_lines = []

target_version = ARGV[0]
target_date = ARGV[1]
puts "Filtering to results for version #{target_version} on date #{target_date}"

File.open('./test_log.txt') do |log_file|
  log_file.each_line do |line|
    version, date, _num_tests, pass_or_fail = line.split
    next unless [version, date] == [target_version, target_date]
    
    case pass_or_fail
    when "pass"
      pass_lines.push line
    when "fail"
      fail_lines.push line
    else
      $stderr.puts "Invalid test result status: #{line}"
    end
  end
end

puts "Passing lines:"
pass_lines.each do |line|
  puts line
end

puts "Failing lines:"
fail_lines.each do |line|
  puts line
end

Run that with ruby main.rb v1.0 2019-04-10 to get the same output as the Python program.

2

u/Dexteroid Apr 11 '19

yeah, your Python solution is pretty close to what I ended up writing. Good thing you wrote the solution here, people might be benefited from it. Thank you!