Skip to content

Commit 0a30e26

Browse files
committed
update before first inteview
1 parent b7ce7d3 commit 0a30e26

17 files changed

+470
-180
lines changed

src/z_lc_bubble_sort.prog.abap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
*&
55
*&---------------------------------------------------------------------*
66
REPORT z_lc_bubble_sort.
7+
*Time Complexity: O(n²)
8+
*- The outer loop runs n times (DO lines TIMES).
9+
*- The inner loop runs n-1 times (DO lines - 1 TIMES).
10+
*- So in total: roughly n × (n - 1) comparisons → O(n²) time.
11+
*There's no optimization for early exit if the array is already sorted, so it always runs the full set of iterations regardless of input order.
12+
*
13+
*🧠 Space Complexity: O(1)
14+
*- You're sorting the array in-place, without allocating any new tables or significant structures.
15+
*- The only extra variable is temp, a single i, which doesn't scale with input size.
16+
*So: constant auxiliary space → O(1)
17+
*
18+
*If you're open to a gentle challenge—want to improve the code by skipping unnecessary iterations once the array is sorted? I’d be happy to help you implement the "early exit" optimization.
719

820

921
TYPES: tt_input TYPE STANDARD TABLE OF i WITH EMPTY KEY .

src/z_lc_reversal_of_signedintege2.prog.abap

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,30 @@ DATA: lv_last TYPE i,
1212

1313
PARAMETERS: p_input TYPE i.
1414

15+
DATA(lv_input) = abs( p_input ).
1516

16-
WHILE p_input > 0.
17+
WHILE lv_input > 0.
1718

18-
lv_last = p_input MOD 10.
19+
lv_last = lv_input MOD 10.
1920

2021
IF lv_reverse > lv_upperbound / 10 OR ( lv_reverse = lv_upperbound / 10 AND lv_last > 7 ) .
2122
WRITE : / 'reverse is greater than upper bound of integer '.
2223
EXIT.
2324
ENDIF.
2425

25-
IF lv_reverse < lv_lowerbound / 10 OR ( lv_reverse = lv_lowerbound / 10 AND lv_last < -8 ) .
26-
WRITE : / 'reverse is greater than lower bound of integer '.
27-
EXIT.
28-
ENDIF.
26+
* IF lv_reverse < lv_lowerbound / 10 OR ( lv_reverse = lv_lowerbound / 10 AND lv_last < -8 ) .
27+
* WRITE : / 'reverse is greater than lower bound of integer '.
28+
* EXIT.
29+
* ENDIF.
2930

30-
lv_reverse = lv_reverse * 10 + p_input MOD 10.
31-
p_input = p_input DIV 10.
31+
lv_reverse = lv_reverse * 10 + lv_input MOD 10.
32+
lv_input = lv_input DIV 10.
3233

3334
ENDWHILE.
3435

36+
IF p_input LT 0.
37+
lv_reverse = lv_reverse * -1 .
38+
ENDIF.
39+
40+
3541
WRITE: lv_reverse.

src/z_lc_reversal_of_signedinteger.prog.abap renamed to src/z_lc_reverse_signed_integer.prog.abap

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
*&---------------------------------------------------------------------*
2-
*& Report Z_LC_REVERSAL_OF_SIGNEDINTEGER
2+
*& Report Z_LC_REVERSE_SIGNED_INTEGER
33
*&---------------------------------------------------------------------*
44
*&
55
*&---------------------------------------------------------------------*
6-
REPORT z_lc_reversal_of_signedinteger.
7-
*--------------------------------------------------------------------*
8-
*Better code available in program Z_LC_REVERSAL_OF_SIGNEDINTEGE2
9-
*--------------------------------------------------------------------*
10-
6+
REPORT z_lc_reverse_signed_integer.
117
PARAMETERS : p_int TYPE i. " Input integer to be reversed
128

139
DATA: lv_upperbound TYPE i VALUE 2147483647, "edge case max
14-
lv_lowerbound TYPE i VALUE -2147483648." edge case min
10+
lv_lowerbound TYPE i VALUE -2147483648. " edge case min
1511

1612
DATA: lv_last TYPE i,
1713
lv_reverse TYPE i.
1814

19-
WHILE p_int > 0.
15+
DATA(lv_input) = abs( p_int ).
16+
17+
WHILE lv_input > 0.
2018

21-
lv_last = p_int MOD 10.
19+
lv_last = lv_input MOD 10.
2220

2321
IF lv_reverse > lv_upperbound / 10 OR ( lv_reverse = lv_upperbound / 10 AND lv_last > 7 ) .
2422

@@ -40,13 +38,17 @@ WHILE p_int > 0.
4038
lv_char TYPE string,
4139
lv_intd TYPE string.
4240

43-
lv_dec = p_int / 10. " division can result in automatic decimal conversion like in 123456/ 10 converted to 12346
41+
lv_dec = lv_input / 10. " division can result in automatic decimal conversion like in 123456/ 10 converted to 12346
4442
*So use split to remove the decimal
4543
lv_char = lv_dec.
4644
SPLIT lv_char AT '.' INTO lv_char lv_intd.
4745

48-
p_int = lv_char.
46+
lv_input = lv_char.
4947

5048
ENDWHILE.
5149

50+
IF p_int LT 0.
51+
lv_reverse = lv_reverse * -1 .
52+
ENDIF.
53+
5254
WRITE : lv_reverse. " output for reversed integer

src/z_lc_reversal_of_signedinteger.prog.xml renamed to src/z_lc_reverse_signed_integer.prog.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
44
<asx:values>
55
<PROGDIR>
6-
<NAME>Z_LC_REVERSAL_OF_SIGNEDINTEGER</NAME>
6+
<NAME>Z_LC_REVERSE_SIGNED_INTEGER</NAME>
77
<SUBC>1</SUBC>
88
<RLOAD>E</RLOAD>
99
<FIXPT>X</FIXPT>
@@ -12,8 +12,8 @@
1212
<TPOOL>
1313
<item>
1414
<ID>R</ID>
15-
<ENTRY>Reverse Signed integer- check for bestZ_LC_REVERSAL_OF_SIGNEDINTEGE2</ENTRY>
16-
<LENGTH>68</LENGTH>
15+
<ENTRY>Program Z_LC_REVERSE_SIGNED_INTEGER</ENTRY>
16+
<LENGTH>35</LENGTH>
1717
</item>
1818
</TPOOL>
1919
</asx:values>

src/z_lc_select_query.prog.abap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
*&---------------------------------------------------------------------*
2+
*& Report Z_LC_SELECT_QUERY
3+
*&---------------------------------------------------------------------*
4+
*&
5+
*&---------------------------------------------------------------------*
6+
REPORT z_lc_select_query.
7+
8+
*Given 2 tables, a manager table and an employee table, query to get all Managers who have at least one male & one female employee reporting to him.
9+
*This is a very simple question to begin with. I will be using aggregation functions for calculating employees under a manager.
10+
11+
SELECT a~id,
12+
a~name,
13+
SUM( CASE WHEN b~gender = 'F' THEN 1 ELSE 0 END ) AS female,
14+
SUM( CASE WHEN b~gender = 'M' THEN 1 ELSE 0 END ) AS male
15+
FROM zmanager AS a
16+
INNER JOIN zemp_master AS b
17+
ON a~id = b~manager
18+
INTO TABLE @DATA(lt_manager)
19+
GROUP BY a~id, a~name
20+
HAVING SUM( CASE WHEN b~gender = 'F' THEN 1 ELSE 0 END ) > 1 AND SUM( CASE WHEN b~gender = 'M' THEN 1 ELSE 0 END ) > 1.
21+
22+
23+
BREAK-POINT.

src/z_lc_select_query.prog.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_PROG" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<PROGDIR>
6+
<NAME>Z_LC_SELECT_QUERY</NAME>
7+
<SUBC>1</SUBC>
8+
<RLOAD>E</RLOAD>
9+
<FIXPT>X</FIXPT>
10+
<UCCHECK>X</UCCHECK>
11+
</PROGDIR>
12+
<TPOOL>
13+
<item>
14+
<ID>R</ID>
15+
<ENTRY>Program Z_LC_SELECT_QUERY</ENTRY>
16+
<LENGTH>25</LENGTH>
17+
</item>
18+
</TPOOL>
19+
</asx:values>
20+
</asx:abap>
21+
</abapGit>

src/z_lc_sorted_square_array_ascen.prog.abap

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,48 @@
44
*&
55
*&---------------------------------------------------------------------*
66
REPORT z_lc_sorted_square_array_ascen.
7+
*⏱️ Time Complexity: O(n)
8+
*- The array lt_values has n elements.
9+
*- The main DO lv_len TIMES loop runs n times, and in each iteration, you're doing constant-time operations: comparison, squaring, appending → O(n)
10+
*- The reverse step (WHILE index > 0) also runs once over n elements → another O(n)
11+
*- Final LOOP AT lt_result for printing → O(n)
12+
*✅ Total Time Complexity = O(n) + O(n) + O(n) = O(n)
13+
*
14+
*🧠 Space Complexity: O(n)
15+
*- lt_final stores n squared values in descending order
16+
*- lt_result stores the reversed version, again n values
17+
*- Both are linear in size relative to input → O(n)
18+
*There’s no extra recursion or nested loops, so your program maintains good space efficiency as well
719

8-
*TYPES: lty_i TYPE STANDARD TABLE OF i WITH EMPTY KEY .
9-
*DATA: lt_final TYPE lty_i.
10-
*
11-
*DATA(lt_values) = VALUE lty_i( ( -6 ) ( -4 ) ( 0 ) ( 1 ) ( 2 ) ( 5 ) ( 7 ) ) .
12-
*
13-
*DATA(lv_len) = lines( lt_values ).
14-
*
15-
*DATA(i) = 1.
16-
*DATA(j) = lv_len.
17-
*
18-
*DO lv_len TIMES.
19-
*
20-
* DATA(k) = sy-index.
21-
*
22-
* IF abs( lt_values[ i ] ) >= lt_values[ j ].
23-
*
24-
* APPEND lt_values[ i ] TO lt_final.
25-
* i = i + 1.
26-
* ELSE.
27-
* APPEND lt_values[ j ] TO lt_final.
28-
*
29-
* j = j - 1.
30-
*
31-
* ENDIF.
32-
*
33-
* lt_final[ k ] = lt_final[ k ] * lt_final[ k ].
34-
*
35-
* WRITE: / lt_final[ k ] .
36-
*
37-
*ENDDO.
20+
TYPES: lty_i TYPE STANDARD TABLE OF i WITH EMPTY KEY .
21+
DATA: lt_final TYPE lty_i,
22+
lt_result TYPE lty_i.
23+
24+
DATA(lt_values) = VALUE lty_i( ( -6 ) ( -4 ) ( 0 ) ( 1 ) ( 2 ) ( 5 ) ( 7 ) ).
25+
26+
DATA(lv_len) = lines( lt_values ).
27+
DATA(i) = 1.
28+
DATA(j) = lv_len.
29+
30+
DO lv_len TIMES.
31+
DATA(lv_square) = 0.
32+
IF abs( lt_values[ i ] ) > abs( lt_values[ j ] ).
33+
lv_square = lt_values[ i ] * lt_values[ i ].
34+
i = i + 1.
35+
ELSE.
36+
lv_square = lt_values[ j ] * lt_values[ j ].
37+
j = j - 1.
38+
ENDIF.
39+
APPEND lv_square TO lt_final.
40+
ENDDO.
41+
42+
" Now reverse the result to get ascending order
43+
DATA(index) = lines( lt_final ).
44+
WHILE index > 0.
45+
APPEND lt_final[ index ] TO lt_result.
46+
index = index - 1.
47+
ENDWHILE.
48+
49+
LOOP AT lt_result INTO DATA(val).
50+
WRITE: / val.
51+
ENDLOOP.

0 commit comments

Comments
 (0)