One thing that I’ve noticed from programming (especially in Java) is that void methods that alter the state of an argument are often very dangerous, even with proper contracting. Generally with an OO language, you want a method that’s a mutator to act on the object it’s attached to, not the objects its given.
Performing foo.bar(baz) (assuming it’s a mutator, and not to retrieve info) should use baz to alter foo with bar as a facilitator. For this reason, I wouldn’t pass the data structure into everything. There are, of course, exceptions to this, mostly in the case of static functions to be used as “library” functions, for instance, Collections.sort() isn’t going to modify Collections, that’s ridiculous, since Collections is a class*, but it is a library function meant to operate on Objects that implement the Collections interface. However, you usually want these to be defined in the super class, or the data structure itself, not in some random place like Main.
For this reason, I’d say use either an encapsulating mutator class with the methods you need, and pass the data structure in the constructor:
Foo bar = new Foo(dataStructure);
foo.doStuff(args)
foo.doMoreStuff()
System.out.println(foo.getStuff());
Or make the data structure itself a Singleton, or register it with a Blackboard**, and have the functions work within those confines. Obviously, like everything, there are situations where violating this may make things cleaner, but for a pre-interview question theory is often more important, you can pontificate on the drawback of a philosophy in an interview where you have a chance to debate.
If I misunderstood, and you’re simply using the data structure to calculate things, without mutating it, then it’s probably fine (and even encouraged) to pass it in.
- Okay, let’s not deal with reflection.
** Well, okay, Blackboards themselves are usually Singletons, but that’s not the point.