programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Bear Bibeault
• Junilu Lacar
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• salvin francis
• Frits Walraven
Bartenders:
• Scott Selikoff
• Piet Souris
• Carey Brown

# Exam question for a beginner coder

Greenhorn
Posts: 16

Mike Simmons wrote: Not unless you've changed it since you posted it above

I did

Marshal
Posts: 15875
265
Mike is right, your algorithm for finding the largest number in an array is not quite right.

Here are some guidelines that will help you come up with a better approach:
1. You may only iterate through the entire array once.
2. You may only look at one array element at a time (your current algorithm looks at two elements on each iteration).
3. Besides the array and (possibly) a loop index, you may use only one other variable in your algorithm

Imagine solving this problem with real-world objects. The array is a row of boxes, each filled with an unknown random number of marbles. You are asked to find out what is the most number of marbles in any of the boxes. Basically the same rules apply: you may only count the marbles in one box at a time. When you're done with a box, you may never look at it again. You are not allowed to memorize or remember anything about a box or its contents after you are done looking at it. The only "memory device" you are allowed to use is a small notepad. You are given a pencil which has an eraser so you can erase your current note before jotting down a new note.

Given these rules, what is the most straightforward way you can think of to figure out the most number of marbles in any of the boxes.

Marshal
Posts: 70228
282
• 1

Junilu Lacar wrote: . . . The only "memory device" you are allowed to use is a small notepad. You are given a pencil which has an eraser . . .

Those three pieces of equipment are very effective tools for working out any algorithm. You may need a large sheet of paper, however.

Rancher
Posts: 3625
40

Joe Pegler wrote:

Mike Simmons wrote: Not unless you've changed it since you posted it above

I did

Then why are you asking if it's OK, if you haven't shown us the latest version? What you've shown does not work, as you can easily verify with the test i described.

 Quick! Before anybody notices! Cover it up with this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton