27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Solutions</strong> to Chapter 12 | Testing<br />

12.2 You are given <strong>the</strong> source to an application which crashes when it is run. After running<br />

it ten times in a debugger, you find it never crashes in <strong>the</strong> same place. The application<br />

is single threaded, <strong>and</strong> uses only <strong>the</strong> C st<strong>and</strong>ard library. What programming errors<br />

could be causing this crash? How would you test each one?<br />

SOLUTION<br />

pg 70<br />

The question largely depends on <strong>the</strong> type of application being diagnosed. However, we can<br />

give some general causes of r<strong>and</strong>om crashes.<br />

1. R<strong>and</strong>om variable: The application uses some r<strong>and</strong>om number or variable component<br />

which may not be fixed for every execution of <strong>the</strong> program. Examples include: user<br />

input, a r<strong>and</strong>om number generated by <strong>the</strong> program, or <strong>the</strong> time of day.<br />

2. Memory Leak: The program may have run out of memory. O<strong>the</strong>r culprits are totally<br />

r<strong>and</strong>om for each run since it depends on <strong>the</strong> number of processes running at that<br />

particular time. This also includes heap overflow or corruption of data on <strong>the</strong> stack.<br />

It is also possible that <strong>the</strong> program depends on ano<strong>the</strong>r application / external module that<br />

could lead to <strong>the</strong> crash. If our application, for example, depends on some system attributes<br />

<strong>and</strong> <strong>the</strong>y are modified by ano<strong>the</strong>r program, <strong>the</strong>n this interference may lead to a crash. Programs<br />

which interact with hardware are more prone to <strong>the</strong>se errors.<br />

In an interview, we should ask about which kind of application is being run. This information<br />

may give you some idea about <strong>the</strong> kind of error <strong>the</strong> interviewer is looking for. For example,<br />

a web server is more prone to memory leakage, whereas a program that runs close to <strong>the</strong><br />

system level is more prone to crashes due to system dependencies.<br />

CareerCup.com<br />

2 1 0

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!