Quantcast
Channel: Tweaks
Viewing all articles
Browse latest Browse all 66

SORTing Algorithm – Interview Question

$
0
0

In our SAP Technical Telegram Group, we discuss everything about SAP. UI5, OData, Fiori issue, New ABAP syntax, CDS approaches, Performance Tuning debates. You think of a topic, we discuss it in our group. No wonder, we are one of the largest SAP Technical group with zero spamming.

Yesterday, one member asked the below question. It was no doubt an interview question from one of the largest IT Company in the world.

Question: How to write the entries of first and second table to third table without SORT statement?

No SORT statement should be there in the solution.

The moment this question was floated, there was a debate among the members asking what is the business requirement? Why can we not use SORT?

Then the member who asked this question revealed it is one of the interview question from one the Big Multi National IT Companies.

Then the next debate started that we should not ask such questions in interview. But one of the member defended saying that SORTing Algorithms are asked by interviewers to eliminate at the very first stage of the selection process.

Ok, leaving behind the debate, whether such interview questions actually test the programming skill or not, everyone agreed to find a solution.

Also Read: Open SQL, CDS or AMDP, which Code to Data Technique to use?

We received 2 approaches with working code snippet.

Solution 1 – Provided by Sercan Küçükdemirci

<code>TYPES: tt_int TYPE TABLE OF i WITH EMPTY KEY.

DATA(it_one) = VALUE tt_int( ( 1 ) ( 9 ) ( 12 ) ( 16 ) ).
DATA(it_two) = VALUE tt_int( ( 4 ) ( 10 ) ( 15 ) ( 20 ) ).

DATA:it_three TYPE tt_int.

APPEND LINES OF it_one TO it_three.
APPEND LINES OF it_two TO it_three.

DATA(lv_len) = lines( it_three ).
DATA(i) = 1.
WHILE i &lt; lv_len.
  DATA(lv_min) = it_three[ i ].
  DATA(j) = i + 1.
  WHILE j &lt; lv_len + 1 .
    IF it_three[ j ] &lt; lv_min.
      DATA(lv_temp) = lv_min.
      lv_min = it_three[ j ].
      it_three[ j ] = lv_temp.
    ENDIF.
    j = j + 1.
  ENDWHILE.
  it_three[ i ] = lv_min.
  i = i + 1.
ENDWHILE.</code>

Team SAPYard validated the output. It is as per the requirement. Thank you Sercan!! You may connect with Sercan at LinkedIn.

Solution 2 – Provided by Stephan Koester

<code>TYPES:
  gtyt_interger        TYPE TABLE OF i WITH EMPTY KEY,
  gtyt_interger_sorted TYPE SORTED TABLE OF i with NON-UNIQUE KEY table_line.

DATA(lt_table1) = VALUE gtyt_interger( ( 1 ) ( 9 ) ( 12 ) ( 16 ) ).
DATA(lt_table2) = VALUE gtyt_interger( ( 4 ) ( 10 ) ( 15 ) ( 20 ) ).

DATA(lt_table3) = CORRESPONDING gtyt_interger_sorted( lt_table1 ).
INSERT LINES OF lt_table2 INTO TABLE lt_table3.</code>

Check the output.

Just the INSERT statement did the Magic!! Amazing solution.

Also Read:My First Program in S/4HANA

Every developer think differently. And there can be different ways to achieve the same solution. Solution to this question is a perfect example.

Our SAP Technical Group is just an example of how we can collaborate and learn every day. No question is too silly to ask. Ask, Answer, Propose, Depend, Share and Learn is the motto of our SAP Group.

This is the exact code snippet and output shared by Stephan. All of us can keep this for our quick reference for new ABAP syntax and usage.

Solution 3 – Provided by Stephan Koester

Stephan provided yet another solution without using INSERT statement. Check the below snippet.

<code>TYPES:
  gtyt_interger        TYPE TABLE OF i WITH EMPTY KEY,
  gtyt_interger_sorted TYPE SORTED TABLE OF i WITH NON-UNIQUE KEY table_line.

DATA(lt_table1) = VALUE gtyt_interger( ( 1 ) ( 9 ) ( 12 ) ( 16 ) ).
DATA(lt_table2) = VALUE gtyt_interger( ( 4 ) ( 10 ) ( 15 ) ( 20 ) ).

DATA(lt_table3) = CORRESPONDING gtyt_interger_sorted( BASE ( lt_table1 ) lt_table2 ).
* Starting with NW7.51 It is possible to prevent duplicate entries using DISCARDING DUPLICATES
* https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/index.htm

cl_demo_output=>write_data( value = lt_table1 name = 'First Table' ).
cl_demo_output=>write_data( value = lt_table2 name = 'Second Table' ).
cl_demo_output=>write_data( value = lt_table3 name = 'Third Table' ).
cl_demo_output=>display( ).</code>

He has used the new BASE key word. BASE is a functional operand position in which a database convertible to the target type can be specified. BASE is use to specify a start value base for the new structure of internal table.

You can read more about keyword BASE in SAP ABAPDOCU

Also, we can use DISCARDING DUPLICATES keywords for handling duplicate rows in component operator CORRESPONDING. Check more about DISCARDING DUPLICATES in ABAP Help.

You may follow Stephan Koester at LinkedIn. You may also check Stephan’s website Koester Consulting for more SAP blogs.

Would you like to share some weird Interview Questions you faced? Anything.. Technical or Non-Technical.

Please share.

Join our Telegram SAP Technical Discuss Group.  You need to install Telegram App first.

Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.

Check HANA-ABAP Tutorials


Viewing all articles
Browse latest Browse all 66

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>