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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 19 | Moderate<br />

________________________________________________________________pg 273<br />

19.9 Since XML is very verbose, you are given a way of encoding it where each tag gets<br />

mapped to a pre-defined integer value. The language/grammar is as follows:<br />

Element --> Element Attr* END Element END [aka, encode <strong>the</strong> element<br />

tag, <strong>the</strong>n its attributes, <strong>the</strong>n tack on an END character, <strong>the</strong>n<br />

encode its children, <strong>the</strong>n ano<strong>the</strong>r end tag]<br />

Attr --> Tag Value [assume all values are strings]<br />

END --> 01<br />

Tag --> some predefined mapping to int<br />

Value --> string value END<br />

Write code to print <strong>the</strong> encoded version of an xml element (passed in as string).<br />

FOLLOW UP<br />

Is <strong>the</strong>re anything else you could do to (in many cases) compress this even fur<strong>the</strong>r?<br />

________________________________________________________________pg 275<br />

19.10 Write a method to generate a r<strong>and</strong>om number between 1 <strong>and</strong> 7, given a method<br />

that generates a r<strong>and</strong>om number between 1 <strong>and</strong> 5 (i.e., implement r<strong>and</strong>7() using<br />

r<strong>and</strong>5()).<br />

________________________________________________________________pg 277<br />

19.11 Design an algorithm to find all pairs of integers within an array which sum to a specified<br />

value.<br />

________________________________________________________________pg 278<br />

CareerCup.com<br />

9 0

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

Saved successfully!

Ooh no, something went wrong!