All Questions
12 questions
3
votes
1
answer
456
views
Are recursions intrinsically less safe than iterations?
Java doesn't have a predefined recursion depth limit. As a result the recursion below (a dummy method that returns the value) throws java.lang.StackOverflowError after 62844 (with static) and 14002 (...
-2
votes
1
answer
353
views
Problem on recursion
void function(int x){
if(x<=0)
return;
function(x--);
}
This is a recursion function which is called with the value of x = 20.
The Recursive call will take place in this way
...
4
votes
1
answer
585
views
Why does `length - 2` recursively give you the center of a linked list?
I am reading through an Algorithms book and am working through a recursive solution to the following question:
Implement a function to check if a linked list is a palindrome
This is an easy enough ...
4
votes
4
answers
21k
views
How does recursive backtracking work ?
I am trying to figure out recursive backtracking, i have good understanding of recursion and till some extent the concept of backtracking too but i am having difficulty understand the chronological ...
3
votes
1
answer
2k
views
What are the time and space complexities of this recursive method that reverses a singly linked list?
What are the Time and Space complexities of this Java method that reverses a singly linked list (of length n)?
I'm more interested in knowing the reasoning behind the space complexity. Let me know if ...
3
votes
5
answers
6k
views
Robot in a grid
This was recently asked to someone I know.
A robot has to move in a grid which is in the form of a matrix. It can go to
A(i,j)--> A(i+j,j) (Down)
A(i,j)--> A(i,i+j) (Right)
Given ...
104
votes
5
answers
50k
views
Why doesn't Java have optimization for tail-recursion at all?
From what I have read: The reason is because it is not easy to determine which method will actually be called as we have inheritance.
However, why doesn't Java at least have tail-recursion ...
0
votes
1
answer
173
views
Better way to create samples
I've done this piece of code for creating bernulli samples, but I think that it is a so heavy algorithm because every time I call this recursive function I create a new vector that is passed to it. Is ...
1
vote
1
answer
636
views
Decrement operator difference between C++ and Java? [closed]
Please tell me why the same piece of code behaves differently in C++ and JAVA.
Ok first I implement a function to calculate the factorial of an Int RECURSIVELY
In JAVA:
int f(int x)
{
...
5
votes
1
answer
724
views
Is this an example of recursion?
I am implementing inheritance of something called Contexts. Each Context holds a link to its parent context. If that Context is the root Context, its parent is null.
In order to walk up the ...
6
votes
4
answers
16k
views
Is there a way to display the stack during recursion method?
I'm trying to learn recursion. I copied this bit of code from my book and added the displays to help me trace what the method is doing and when.
public static void main(String[] args) {
...
13
votes
8
answers
58k
views
Can a recursive function have iterations/loops?
I've been studying about recursive functions, and apparently, they're functions that call themselves, and don't use iterations/loops (otherwise it wouldn't be a recursive function).
However, while ...