Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

Kevin Mckeon

Ranch Hand
+ Follow
since Jan 17, 2018
Cows and Likes
Cows
Total received
4
In last 30 days
0
Total given
0
Likes
Total received
4
Received in last 30 days
0
Total given
6
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kevin Mckeon

Campbell Ritchie wrote:The method reference is one way to pass a λ expression. Are you not familiar with method references? If you go through the Java™ Tutorials, you will find out that you are not passing the method (as you might in C), but that is part of the way you can define an object that uses that method.

Adding discussion to what used to be the Java8 forum.



Is there a way of doing this without using λ expression ?

Ive tried to pass/call the method by :




And by changing the method Declaration :









But No luck
6 months ago
I am working on a Coding problem from an Online IDE :

Given a string, reverse defined segments and return the new string.

EG : Given Input s1 = "abcd\nefgh\nijkl\nmnop", Return output : s2 = "dcba\nhgfe\nlkji\nponm"

The Question itself is easy, but I cant figure out the default code in the IDE :



And the test case for this code :



I cant understand the lines :

or


Why would anyone try to pass the method to be tested, along with the parameter that the same Method is going to use for testing to a second completely unrelated method.
6 months ago

Campbell Ritchie wrote:If I understood the question properly, n²



Sorry this is the scenario I mean

6 months ago

Campbell Ritchie wrote:Question maybe a bit scary for the “beginning” forum, so I think I shall move you.

You wouldn't pass “Array[]” to a method, but “Array”. Once you work that out, yes the whole code runs in quadratic complexity (=n²) as its upper limit. It might behave more like linear if the size of the array is very small, but complexity is calculated on a worst‑case basis, so it is correct ot say quadratic.



So in that case, if you had 3 for loops nested, the complexity would be N cubed.

But what if you had 4 For loops in total, a nested loop at the beginning of the program and a different nested for loop at the end of the program.

Would the complexity still be N squared ? Or N^4 now ?
6 months ago
I've created a pseudo-code version of a problem I was given in an interview



Would I be right in saying the complexity is Order N squared or : O(n^2)

The first forLoop is run a constant amount of times so N doesn't change.
The second loop runs N times also, but since it calls a function which contains a third loop, Its essentially a nested loop N*N which is N squared ?

Finally, after writing an algorithm like this. How often do companies quiz you on the complexity of your loops etc. ?

6 months ago
Every time I start a problem involving date or time, I google "working with date/time" and get a different set of options.

Normally I would take a Youtube course in an unfamiliar topic but i feel that with Date/Time, any tutorial could be outdated.

From a quick search, I found the following classes :



Sometimes my eclipse flags and error or issue that a particular method or class is no longer supported
7 months ago
When creating a portfolio to show off, I normally use eGit to track changes in my eclipse project and then push changes to GitHub. Its usually problem free unless I make a mess of something, and i only work on one java project at a time.

But when following a tutorial, I end up with a new workspace and loads of tiny projects (20+) where each project outlines one aspect of a technology for example :

- SpringTutorial1 -  Setting Bean Properties
- SpringTutorial2 -   Factory Beans and Methods
- SpringTutorial3 -   Init and Destroy Methods
- SpringTutoria4 -   Dependency Injection
- etc..

I want to push my whole Workspace to GitHub so that when I add a small project, I can keep the repo up to date and if my PC crashes, all is not lost.

I am not very familiar with Git, but I dont think I can use eGit or Eclipse for this (because its not an individual project). I think I need to use Command Line and do git-Init into the workspace folder and try gitIgnore so not every file is added.

Is this correct ? or is this the wrong way of working with Git?
8 months ago
I'm trying to set up a simple database to use with a website and running into problem after problem.

I set downloaded SQLPlus and SQLDeveloper a few months ago and have since forgotten passwords etc.

When I open SQL developer, I have one database in my list of connections called ORCL. I cant expand this because I don't have the PASSWORD associated with it. (I believe the Username/database is the same thing and is ORCL right ?)

So I open SQLPlus, try and log in 'SYS AS DBASYS'

When I try and reset the password for ORCL I get issues :

Tim Nachreiner wrote:Probably not a good idea to make your Bean ID with spaces in it: jsp view resolver.   Try making it one word: view-resolver or viewresolver.



True but my own stupidity was the  real problem here

was spelt wrong so it wasnt picking up the right class to when scanning for annotations

also a typo in means it was searching for a folder that doesnt exist.

Need to be more careful
10 months ago
I get some version of this issue pretty much every time I create an App whether I use Spring or Simple Web Apps

I am getting a 404 on the web page and this error and I cant tell why :

Sep 26, 2019 5:40:31 P.M. org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Sep 26, 2019 5:40:31 P.M. org.apache.catalina.startup.Catalina start
INFO: Server startup in 3015 ms
Sep 26, 2019 5:40:52 P.M. org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/Spring_Udemy_01_-_SpringMVC/] in DispatcherServlet with name 'offers'
Sep 26, 2019 5:41:17 P.M. org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/Spring_Udemy_01_-_SpringMVC/] in DispatcherServlet with name 'offers'


Note : The Index.jsp page loads fine when I pull it out of the folder to the root folder as expected. But I want the home.jsp within the 'jsps' folder to be the page shown when starting the app.

I cant see any configuration issues :


10 months ago

Piet Souris wrote:

Let s[n] denote the cheapest solution when we put n persons into one room. If I am not mistaken, I get this list:

s[1] = 95
s[2] = 140
s[3] = 210
s[4] = 400

And let a[n] denote the cheapset solution when we are dealing with a group of n persons. So, a[0] = 0, since that costs nothing.

We get:

a[1] = s[1]
      = 95

a[2] = cheapest of a[1] + s[1] = 190
                   a[0] + s[2] = 140

a[3] = cheapest of a[2] + s[1] = 235
                   a[1] + s[2] = 235
                   a[0] + s[3] = 210

a[4] = cheapest of a[3] + s[1] = 305
                   a[2] + s[2] = 280
                   a[1] + s[3] = 305
                   a[0] + s[4] = 400

et cetera

And since there is no s[5] and higher, we only have to consider the four steps as in a[4]:

a[5] = cheapest of a[4] + s[1]
                   a[3] + s[2]
                   a[2] + s[3]
                   a[1] + s[4]



This looks like a fairly straightforward nested loops question on first look but when trying to program it its actually damn hard !

How would you do this ? Nested loops gets very messy with i's and j's all over the place. Is there an easier structure to use ?

10 months ago
I think ill leave that problem to another day ! But thanks for all your help - its been great. Ill post up the link to my Source code after I finish if people want to view it

One last question I have is - This question was given to us as an example of a typical interview question for a software developer

What level of developer would it be aimed at ?

And assuming it was given to applicants of all levels how far would they expect someone to get ?

A senior dev should be able to knock out a reasonable solution in a few hours I would guess.  I would class myself and somewhere between Junior and Intermediate developer but its difficult to know whats expected.

Its taken me a long time to get to a level where I understand and can now make a decent attempt to solve. But it would still take hours of work to come up with a solution without help!

My project has 5 files.
  • App - Does the dynamic populating the arrays and finding the cheapest values
  • Room - Room object
  • RoomUtils - Finds the cheapest room for 1,2,3,4 people and other methods
  • CheapestRoomObject - Stores the details of the cheapest room
  • FileUtils - Reads in the Excel file and creates a list of room Objects


  • 10 months ago
    Fantastic, this makes perfect sense. This is what I was getting near in my head but was struggling to get in down on paper.

    I have one issue with your method though and I think the answer is probably straightforward with one additional check (or otherwise I'm wrong)


    s[1] = 95
    s[2] = 140
    s[3] = 210
    s[4] = 400

    a[1] = s[1]
         = 95

    a[2] = cheapest of a[1] + s[1] = 190
                               a[0] + s[2] = 140

    a[3] = cheapest of a[2] + s[1] = 235
                               a[1] + s[2] = 235
                               a[0] + s[3] = 210

    a[4] = cheapest of a[3] + s[1] = 305
                               a[2] + s[2] = 280
                               a[1] + s[3] = 305
                               a[0] + s[4] = 400

    et cetera


    Lets just say for arguments sake (without me actually changing the figures) that the four lines i have marked red above were the cheapest solution for a[1], a[2] , a[3], a[4]  

    The problem arises when the room a[1] / s[1] only has space for two people.

    By the time the algorithm is computing a[5],  s[1] would have been used in all the previous calculations hence the room would be full before a[3] is even calculated .

    Isn't that right ? Or am I confusing myself even further
    10 months ago
    thanks for the help but im sill having a few issues.

    I've created a method (cheapestForN) to return the for the cheapest room for 1, 2, 3 or 4 persons.
    But trying to fill the array by dynamic programming is proving difficult

    Its had to describe over text so i've attached an image


    (the maximum number of guests allowed in a room is in brackets beside the room number. Green number shows the cheapest option)

    1. The cheapest room for one person is obvious (6117)

    2. The cheapest room for two people is either going to be :
       a. The result of cheapestForN(2) = 6096 or
       b. Filling cheapestForN(1) with two people

    3. The cheapest room for three people is either going to be :
       a. Filling cheapestForN(3) = 6145
       b. Filling cheapestForN(2) with three people
       c. Putting two people in room 6096 and one person in room 6117
       d. Putting two people in room 6096 and one person in room 6145
       e. Some other combination that I cant see at the moment

    As far as i can see, moving from a[1] to a[2] onto a[3] and onto a[4] - the number of combination checks grow exponentially which defeats the purpose of dynamic programming

    Even as I type I am beginning to see that the options i have put in bold above are not viable because of the results in a[1] and a[2]. Maybe i am doing it right after all.
    10 months ago
    I've been doing a good bit of research into the topic of Dynamic Programming since your reply.

    I want to get a good grasp of DP before tackling this problem.

    I have a good understanding now of the Longest Increasing SubSequence Problem found here Link to Increasing SubSequence Problem

    I have one question, does your method and hint above use the method of Memoization or Tabulation when finding the optimal solution. Is it possible to use both methods and still get a solution ?

    Memoization vs Tabulation

    10 months ago