How Do You Fix a Python Program that Returns “Killed”?

Problem scenario
You are trying to write a Python program. It returns "Killed" on a Linux terminal. How should you fix this?

Possible Solution #1
Refactor your code. There is a programming method called "brute force" which is a reference to creating an exhaustive number of possibilities and processing those possibilities. This method will not work for certain problems because the input size is too large. You may want to read A program can still solve the problem, but you will have to be clever.

Is there a way to represent a string or data type symbolically without creating so many permutations? Can you calculate what you need to find without having every a string or bit of non-string data exist in memory? Sometimes arithmetic can be performed without creating the string in your program. You may be able to find a solution without putting the data in memory.

A sample pattern of a repeating string or larger data type can be processed as long as the pattern is representative of the whole. Then the result could be mathematically computed to determine the full size. This can reduce the amount of memory used.

You may be too mentally focused on one solution. There may be an alternative solution achievable with rewriting your code. For large input sizes, certain computational complexity will exhaust memory. Examine the logic, use Google, and try to find a way to solve the problem with Python without creating such large strings or putting so many variables/objects into memory.

Some sorting algorithms can solve your sorting needs in n(log(n)) time or better than this.

Possible Solution #2
Run "dmesg" from the Linux terminal. This may show you what is wrong. You may need to add memory; to add memory, see this posting. To add swap space, see this posting. It could be that you need to refactor your program to use less memory.

Possible Solution #3
Sometimes writing to disk, if you have the disk space, is one way to solve a Python coding quiz. But these are not very performant solutions. This method uses less memory and can prevent your program from being killed by the Python interpreter.

Possible Solution #4
Keep practicing, and eventually you will be able to create elegant solutions without writing to disk or adding memory. To see a list of Python books, click here.

Leave a comment

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